【问题标题】:PHP Reading big file aborts site loadPHP读取大文件中止站点加载
【发布时间】:2014-05-09 19:26:11
【问题描述】:

我有一个 PHP 脚本,它加载 175KB 文件的内容,但在页面顶部失败。 (它应该适用于较小的文件!!) 这是脚本 sn-p:

        if(!file_exists($sFile) || !is_readable($sFile)) {
            echo "<span style='color: #FF3333;'>Can not find/open file.</span><br />";
        } else {
            if(isset($_GET['content']) && is_writable($sFile)) {
                $hFile = fopen($sFile, "w");
                fwrite($hFile, urldecode($_GET['content']));
                fclose($hFile);
            }

            $sContent = file_get_contents($sFile);
        }
        ?>
        <a href="<?php echo "?page=browser&path=" . dirname(realpath($sFile)); ?>" title="Back to <?php echo dirname(realpath($sFile)); ?>">
            &raquo; Back to <i><?php echo dirname(realpath($sFile)); ?></i>
        </a>

        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
            <input type="hidden" name="page" value="edit" />
            <input type="hidden" name="file" value="<?php echo $sFile; ?>" />
            <textarea name="content" placeholder="..." style="margin: 0; padding: 0; width: 100%; height: 350px; resize: vertical;"><?php echo $sContent; ?></textarea>
            <input type="submit" value="Save changes" style="width: 100%;" />
        </form>

【问题讨论】:

标签: php fopen file-get-contents


【解决方案1】:

从您的问题标题来看,我认为 PHP 的 set_time_limit 将是您的解决方案。但是,在完整阅读您的问题后,它也可能是在 php.ini 内设置的内存限制或服务器具有的硬限制。

您可以尝试改用fopen()fread(),例如:

$f = fopen("yourfile.txt", "r");
$s = "";
while (!feof($f)) {
  $s .= fread($f, 4096);
}
fclose($f);
// do something with the file contents inside of $s

您还可以扩展此示例,以便在读取文件时回显文件。

希望对您有所帮助。

【讨论】:

  • 谢谢,我刚刚注意到内存限制也超过了我树莓派的资源;)
猜你喜欢
  • 1970-01-01
  • 2016-10-08
  • 2013-12-21
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2021-02-02
  • 2017-05-13
  • 1970-01-01
相关资源
最近更新 更多