2012-03-23 76 views
0

我有一个程序需要3个数组(它们的长度相同,可以包含500个左右的数据)并将它们写入文本文件。PHP有保存“大”文件的问题

但是,我遇到了编写大文件的问题。数组是画布绘图应用程序的坐标和时间戳,所以我可以控制长度。我发现,一旦文件开始变得大于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); 

我已经在我的本地设置一个PHP服务器和访问到错误日志。这就是我所得到的。

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

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

+0

您是否在服务器上安装了suhosin补丁程序? – piotrekkr 2012-03-23 12:54:09

+0

正如我所说这是在一个免费的网络服务器,所以不知道 – 2012-03-23 13:05:37

+0

它是显示当你调用'phpinfo();' – chiborg 2012-03-23 13:13:09

回答

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/

+0

如果此文件托管在免费的Web服务器上,是否可以查看Apache错误日志? – 2012-03-23 13:03:57

+0

我认为它应该是'if($ i%100 == 0)':) – piotrekkr 2012-03-23 13:10:42

+0

某些托管提供程序允许您访问自己的日志文件,否则可以通过调用'ini_set将错误输出重定向到您选择的目录('error_log','/path/of/your/choice/error.log');' – chiborg 2012-03-23 13:11:21

1

可能脚本超出最大执行时间。

在你的代码的开头添加这

set_time_limit(0)

+0

试过了,没有任何区别:/ – 2012-03-23 12:55:25

+0

你是否试过在localhost上运行这个脚本? – Leri 2012-03-23 12:58:27

+0

不,因为我没有PHP设置 – 2012-03-23 13:04:49

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); 
+0

看到我对chiborg的回答,如果这是在免费的Web服务器上托管的话,是否可以查看apache错误日志? – 2012-03-23 13:04:27

+0

你可以问根有关配置或至少询问是error_log – 2012-03-23 13:11:06

+0

将查找错误日志 – 2012-03-23 13:18:09

0

PHP(一种因此Web服务器)进行自我保护。也许使用不同的机制来上传一个大文件 - 我会想象他们来自已知(可信任)的来源。使用不同的机制,例如SFTP。