开发者

Is there an alternative to php readfile() in safe mode server?

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


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜