在安全模式服务器中是否有 php readfile() 的替代方案?

发布于 2024-11-15 07:14:36 字数 165 浏览 3 评论 0原文

我将我的网站托管在共享主机上,该主机最近将服务器更改为安全模式(甚至没有通知)。 我使用从服务器下载文件的功能,使用 readfile() 函数(我使用 php)。 现在,在安全模式下,该功能不再可用。 是否有替代方法或解决方法来处理用户可以下载文件的情况?

谢谢

I host my site on a shared hosting, which lately changed the server to safe mode (without even notifying that).
I use a feature that download files from the server, using the readfile() function (I use php).
Now, in safe_mode, this function is no longer available.
Is there a replacement or a workaround to handle the situation that the file will be able to be downloaded by the user?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-11-22 07:14:36

正如我在评论中所写,通过将 readfile() 包含在 disable_functions php.ini 指令中来禁用它。它与安全模式无关。尝试检查哪些函数被禁用,并查看是否可以使用任何其他文件系统函数来执行 readfile() 的操作。

要查看禁用功能的列表,请使用:

var_dump(ini_get('disable_functions'));

您可以使用:

// for any file
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    fpassthru($file);
    fclose($file);
}

// for any file, if fpassthru() is disabled
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    while ( !feof($file) ) {
        echo fread($file, 4096);
    }
    fclose($file);
}

// for small files;
// this should not be used for large files, as it loads whole file into memory
$data = file_get_contents($filename);
if ( $data !== false ) {
    echo $data;
}

// if and only if everything else fails, there is a *very dirty* alternative;
// this is *dirty* mainly because it "explodes" data into "lines" as if it was
// textual data
$data = file($filename);
if ( $data !== false ) {
    echo implode('', $data);
}

As I wrote in comments, readfile() is disabled by including it in disable_functions php.ini directive. It has nothing to do with safe mode. Try checking which functions are disabled and see if you can use any other filesystem function(-s) to do what readfile() does.

To see the list of disabled functions, use:

var_dump(ini_get('disable_functions'));

You might use:

// for any file
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    fpassthru($file);
    fclose($file);
}

// for any file, if fpassthru() is disabled
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    while ( !feof($file) ) {
        echo fread($file, 4096);
    }
    fclose($file);
}

// for small files;
// this should not be used for large files, as it loads whole file into memory
$data = file_get_contents($filename);
if ( $data !== false ) {
    echo $data;
}

// if and only if everything else fails, there is a *very dirty* alternative;
// this is *dirty* mainly because it "explodes" data into "lines" as if it was
// textual data
$data = file($filename);
if ( $data !== false ) {
    echo implode('', $data);
}
和我恋爱吧 2024-11-22 07:14:36

我假设您正在使用 readfile 加载远程文件,正如您所说的“从服务器”。如果这是正确的,那么您的问题不是安全模式,而是不再允许使用普通 php 文件功能打开 URL(设置 allow_url_fopen 禁用)。

在这种情况下,您可以使用 PHP 的 curl 函数来下载文件。此外,file_get_contents 也是一个有效的替代方案。

I assume you are using readfile to load remote files, as you said "from the server". If that is correct, your problem is not safe mode but that opening URLs with normal php file functions is not permitted anymore (setting allow_url_fopen disabled).

In that case, you can use PHP's curl functions to download files. Also, file_get_contents is a valid alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文