dongtan8122 2015-08-04 23:54
浏览 24
已采纳

Icecat和PHP files.index.xml

I have several scripts running that downloads the daily xml and looks for every .xml in it and downloads them to a different folder so

                    1234.xml
                  / 
daily.index.xml - - 4567.xml
                  \
                    6789.xml

Now I wish to do the same with the files.index.xml file, But everytime I try to open the index file the server stops with:

PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 1073217536 bytes)

Is there a way to open up and dissect files.index.xml without my server to crash constantly?

Update: I believe the server hangs somewhere while running the script, as some XML files are beeing stored in the directory

Script:

// 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 logs that 'file_get_contents($url, false, $context);' is leading to max out the memory.

Currently I'm trying to upload the files.index.xml (1,41gb file) in hope that I can process it this way.

  • 写回答

1条回答 默认 最新

  • donglian8407 2015-08-05 00:26
    关注

    Based on the information provided, there are two issues here. The most direct issue is that you're trying to allocate an extra 1GB of memory to your PHP script after it's already reached its 1GB limit (which is much higher than the default limit). Assuming you're using PHP 5.1+, you can use fopen() and file_put_contents() together to buffer the file between HTTP and disk:

    <?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!';  
    }
    

    If you need more control over the buffering, you can fread() a fixed-size buffer from HTTP and fwrite() the buffer to the output file as you read it. You can also use the PHP cURL Extension to download the file, if you'd rather cURL handle the buffering.

    As posted, your code reads the entire remote file into memory, then makes a copy of the whole thing as it writes it into the output file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了