PHP在保存“大”文件时遇到问题

时间:2012-03-23 12:44:39

标签: php

我有一个程序需要3个数组(长度相同,可以包含500个项目)并将它们写入文本文件。

但是我遇到了编写更大文件的问题。数组是画布绘图应用程序的坐标和时间戳,因此我可以控制长度。我发现一旦文件开始大于2mb,它就不会保存文件。我设法保存的最大文件数为2.18mb。从相关问题PHP: Having trouble uploading large files我已经确定原因很可能是由于托管在免费托管服务器上。我看过phpinfo(),这里有4个相关数字:

memory_limit 16M
max_execution_time 30
upload_max_filesize 5M
post_max_size 5M

以下是相关的编写代码:

// retrieve data from the JS
$x_s = $_GET['x_coords'];
$y_s = $_GET['y_coords'];
$new_line = $_GET['new_lines'];
$times = $_GET['time_stamps'];

print_r($_GET);
$randInt = rand(1,1000);

// first want to open a file
$file_name = "test_logs/data_test_" . $randInt . ".txt";
$file_handler = fopen($file_name, 'w') or die("Couldn't connect");

// For loop to write the data
for ($i = 0; $i < count($x_s); $i++){
    // If new line want to write new line!
    if (!$new_line[$i]){
        if ($i!=0){
            // If not the first line
            fwrite($file_handler, "LINE_END\n"); }          
        fwrite($file_handler, "LINE_START\n");
    }

    // Write the x coord, y coord, timestamp
    fwrite($file_handler, $x_s[$i] . ", ". $y_s[$i] .", ". $times[$i]. "\n");

    // If last line then write last LINE_END
    if ($i == (count($x_s)-1)){
        fwrite($file_handler, "LINE_END\n"); }
}

fclose($file_handler);

我在我的localhost上设置了一个php服务器,并且可以访问错误日志。这就是我得到的。

[Fri Mar 23 20:03:02 2012] [error] [client ::1] request failed: URI too long (longer than 8190)

问题已解决:问题是我使用GET发送大量数据,这些数据已附加到URI。一旦URI达到8190个字符,就会出错。使用POST解决了这个问题。

4 个答案:

答案 0 :(得分:1)

脚本可能超过最长执行时间。

添加此

set_time_limit(0)

在代码的开头。

答案 1 :(得分:1)

upload_max_filesizepost_max_size确定可以发布的最大数据大小。但这可能不是你的问题,因为写了一些数据(如果达到数据限制,脚本就不会执行)。

您的脚本有两个限制:max_execution_timememory_limit。查看您的apache错误日志文件,看看是否收到错误消息(说明达到了哪个限制)。

您还可以尝试在for循环中进行日志记录,以查看时间和内存使用情况的进展情况:

if(($i % 100) == 0) { // log every 100 entries
  error_log(date("H:i:s ").memory_get_usage(true)."Bytes used\n", 3, 'test.log');
}

也可能是Suhosin补丁阻止您发送太多数据点: http://www.adityamooley.net/blogs/2012/01/09/php-suhosin-and-post-data/

答案 2 :(得分:1)

1)检查max_input_time

ini_set ( 'max_input_time', 50 );

2)签入phpinfo() - 你有Suhosin补丁吗?

您应该查看apache error_log - 您应该找到达到的限制。

尝试

ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
ini_set('log_errors', true);
ini_set('html_errors', false);
ini_set('error_log', dirname(__FILE__).'script_error.log');
ini_set('display_errors', true);

答案 3 :(得分:0)

PHP(因此是Web服务器)正在保护自己。也许使用不同的机制来上传大文件 - 我想他们来自已知(可信)的来源。使用不同的机制,例如SFTP。

相关问题