PHP: readfile() has been disabled for secu

2019-08-06 03:25发布

I wrote a php script which outputs html files on the screen which uses readfile($htmlFile); however in the web-hosting that I have purchased the readfile() has been disabled for security reasons. Is there any substitution ( other php functions) for the readfile() or I have no choice but to ask the admin to enable it for me?

Thanks

标签: php security
3条回答
不美不萌又怎样
2楼-- · 2019-08-06 03:59

You can check which functions are disabled by using:

var_dump(ini_get('disable_functions'));

You can try to use fopen() and fread() instead:

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

Or fopen() with fpassthru()

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

Alternatively you can use fwrite() to write content.


You can also try to use file_get_contents()

http://nl2.php.net/file_get_contents

Or you can use file()

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

I wouldn't recommend this method though, but if nothing works...

$data = file($filename);
if ( $data !== false ) {
    echo implode('', $data);
}
查看更多
太酷不给撩
3楼-- · 2019-08-06 04:07

If its disabled then you could do something like following as alternative:


$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

Hope it helps

查看更多
Melony?
4楼-- · 2019-08-06 04:12

You can try :

$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;
查看更多
登录 后发表回答