PHP:readfile()出于安全原因被禁用

时间:2012-02-15 08:20:27

标签: php security

我写了一个php脚本,在屏幕上输出html文件,使用readfile($ htmlFile); 但是在我购买的web托管中,出于安全原因,readfile()已被禁用。 是否有任何替换(其他PHP函数)的readfile()或我别无选择,但要求管理员为我启用它?

由于

3 个答案:

答案 0 :(得分:2)

您可以使用以下方法检查禁用的功能:

var_dump(ini_get('disable_functions'));

您可以尝试使用fopen()和fread()代替:

http://nl2.php.net/manual/en/function.fopen.php

http://nl2.php.net/manual/en/function.fread.php

$file = fopen($filename, 'rb');
if ( $file !== false ) {
    while ( !feof($file) ) {
        echo fread($file, 4096);
    }
    fclose($file);
}

使用fpassthru()

进行fopen()
$file = fopen($filename, 'rb');
if ( $file !== false ) {
    fpassthru($file);
    fclose($file);
}

或者,您可以使用fwrite()来编写内容。


您也可以尝试使用file_get_contents()

http://nl2.php.net/file_get_contents

或者您可以使用file()

http://nl2.php.net/manual/en/function.file.php

我不会推荐这种方法,但如果没有效果......

$data = file($filename);
if ( $data !== false ) {
    echo implode('', $data);
}

答案 1 :(得分:1)

如果它被禁用,那么你可以做以下的事情作为替代:


$file = fopen($yourFileNameHere, 'rb');
if ( $file !== false ) {
    while ( !feof($file) ) {
        echo fread($file, 4096);
    }
    fclose($file);
}

//OR
$contents = file_get_contents($yourFileNameHere); //if for smaller files

希望有所帮助

答案 2 :(得分:1)

您可以尝试:

$path = '/some/path/to/file.html';
$file_string = '';
$file_content = file($path);
// here is the loop
foreach ($file_content as $row) {
     $file_string .= $row;
}

// finally print it
echo $file_string;
相关问题