【问题标题】:PHP: readfile() has been disabled for security reasonsPHP:出于安全原因,readfile() 已被禁用
【发布时间】:2012-02-15 08:20:27
【问题描述】:

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

谢谢

【问题讨论】:

  • 移动主机,他们显然不是一个好人,谁知道他们还禁用了什么。
  • 这听起来像是一件奇怪的事情要禁用 - 通常是 system() 调用出于安全原因而被禁用。它是一种什么样的安装方式?据我所知,这对于 cPanel(一种流行的 Linux 共享主机系统)来说是不正常的。
  • 顺便说一句,是的,请与主机联系 - 并要求他们提供禁用功能的完整列表并将它们粘贴到此处。有人会告诉您它们是否合理(尽管完全有可能在没有禁用任何内容的情况下运行共享主机,afaik)。
  • 以下是禁用的函数列表:allow_url_fopen、escapeshellarg、escapeshellcmd、ini_alter、passthru、popen、proc_open、proc_close、proc_terminate、proc_get_status、proc_nice、readfile、show_source、system

标签: php security


【解决方案1】:

您可以使用以下命令检查哪些功能被禁用:

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

或者 fopen() 和 fpassthru()

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

【讨论】:

    【解决方案2】:

    如果它被禁用,那么您可以执行以下操作:

    $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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      你可以试试:

      $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;
      

      【讨论】:

        猜你喜欢
        • 2014-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多