我有几个脚本正在运行,它们下载每日xml并查找其中的每个.xml,并将它们下载到不同的文件夹中,如下所示
1234.xml
/
daily.index.xml - - 4567.xml
\
6789.xml现在,我希望对files.index.xml文件也这样做,但是每次我尝试打开索引文件时,服务器都会停止:
PHP致命错误:允许内存大小为1073741824字节(试图分配1073217536字节)
有没有一种方法可以在没有服务器的情况下打开和剖析files.index.xml,使其不断崩溃?
更新:我相信服务器在运行脚本时挂在某个地方,因为有些XML文件存储在目录中。
剧本:
// URL for index file
$url = "http://data.icecat.biz/export/level4/EN/files.index.xml";
// Custom header (username/pass is a paid account, so I can't share the credentials)
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("username:pass")
)
));
// Get XML File
$indexfile = file_get_contents($url, false, $context);
// Save XML
$file = '../myhomeservices/fullindex/files_index.xml';
unlink($file);
$dailyfile = fopen("../myhomeservices/fullindex/files_index.xml", "w") or die("Unable to open file!");
chmod($dailyfile, 0777);
// Write the contents back to the file
$dailyxmlfile = fwrite($dailyfile, $indexfile);
if($dailyxmlfile){
} else {
echo 'Error!';
}
fclose($myfile);enter code hereApache日志“file_get_contents($url,false,$context)”将导致内存最大化。
目前,我正试图上传files.index.xml (1,41 it文件),希望我可以这样处理它。
发布于 2015-08-05 00:26:40
根据所提供的资料,这里有两个问题。最直接的问题是,在PHP脚本已经达到1GB的限制(这远远高于默认的限制)之后,您正在尝试为PHP脚本分配额外的1GB内存。假设您使用的是PHP,您可以同时使用5.1+和file_put_contents()来缓冲HTTP之间的文件:
<?php
$url = "http://data.icecat.biz/export/level4/EN/files.index.xml";
// Custom header (username/pass is a paid account, so I can't share the credentials)
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("username:pass")
)
));
$file = '../myhomeservices/fullindex/files_index.xml';
@unlink($file);
chmod($file, 0777);
// Write the contents back to the file
if (!file_put_contents($file, fopen($url, 'r', false, $context)))
{
echo 'Error!';
}如果您需要对缓冲区进行更多的控制,您可以在读取缓冲区时,从fread()和fwrite()将一个固定大小的缓冲区发送到输出文件。您还可以使用PHP扩展名下载文件,如果您希望cURL处理缓冲的话。
如前所述,您的代码将整个远程文件读入内存,然后在将其写入输出文件时复制整个文件。
https://stackoverflow.com/questions/31821260
复制相似问题