PHP阅读大文件中止网站加载

时间:2014-05-09 19:26:12

标签: php fopen file-get-contents

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

        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>

1 个答案:

答案 0 :(得分:2)

根据您问题的标题判断,我说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

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

希望有所帮助。