首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Icecat & PHP files.index.xml

Icecat & PHP files.index.xml
EN

Stack Overflow用户
提问于 2015-08-04 23:54:17
回答 1查看 830关注 0票数 1

我有几个脚本正在运行,它们下载每日xml并查找其中的每个.xml,并将它们下载到不同的文件夹中,如下所示

代码语言:javascript
复制
                    1234.xml
                  / 
daily.index.xml - - 4567.xml
                  \
                    6789.xml

现在,我希望对files.index.xml文件也这样做,但是每次我尝试打开索引文件时,服务器都会停止:

PHP致命错误:允许内存大小为1073741824字节(试图分配1073217536字节)

有没有一种方法可以在没有服务器的情况下打开和剖析files.index.xml,使其不断崩溃?

更新:我相信服务器在运行脚本时挂在某个地方,因为有些XML文件存储在目录中。

剧本:

代码语言:javascript
复制
// 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 here

Apache日志“file_get_contents($url,false,$context)”将导致内存最大化。

目前,我正试图上传files.index.xml (1,41 it文件),希望我可以这样处理它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-05 00:26:40

根据所提供的资料,这里有两个问题。最直接的问题是,在PHP脚本已经达到1GB的限制(这远远高于默认的限制)之后,您正在尝试为PHP脚本分配额外的1GB内存。假设您使用的是PHP,您可以同时使用5.1+和file_put_contents()来缓冲HTTP之间的文件:

代码语言:javascript
复制
<?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处理缓冲的话。

如前所述,您的代码将整个远程文件读入内存,然后在将其写入输出文件时复制整个文件。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31821260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档