如果文件大于给定大小,则阻止从远程源加载 [英] Prevent loading from remote source if file is larger than a given size

查看:70
本文介绍了如果文件大于给定大小,则阻止从远程源加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要从远程服务器加载最多10MB的XML文件。

Let's say I want XML Files only with upto 10MB to be loaded from a remote server.

类似

$xml_file = "http://example.com/largeXML.xml";// size= 500MB

//PRACTICAL EXAMPLE: $xml_file = "http://www.cs.washington.edu/research/xmldatasets/data/pir/psd7003.xml";// size= 683MB

 /*GOAL: Do anything that can be done to hinder this large file from being loaded by the DOMDocument without having to load the File n check*/

$dom =  new DOMDocument();

$dom->load($xml_file /*LOAD only IF the file_size is <= 10MB....else...echo 'File is too large'*/);

可能如何实现?另类?或达到此目的的最佳方法将受到高度赞赏。

How can this possibly be achieved?.... Any idea or alternative? or best approach to achieving this would be highly appreciated.

我检查了 PHP :远程文件大小而无需下载文件,但是当我尝试类似

I checked PHP: Remote file size without downloading file but when I try with something like

var_dump(
    curl_get_file_size(
        "http://www.dailymotion.com/rss/user/dialhainaut/"
    )
);

我得到字符串'unknown'(长度= 7)

当我尝试使用以下建议的 get_headers 时,标头中缺少Content-Length,因此也无法可靠地工作。

When I try with get_headers as suggested below, the Content-Length is missing in the headers, so this will not work reliably either.

请告知如何确定长度,并避免将其发送到 DOMDocument ,如果它超过了 10MB

Please kindly advise how to determine the length and avoid sending it to the DOMDocument if it exceeds 10MB

推荐答案

好,终于可以了。标头解决方案显然无法广泛使用。在此解决方案中,我们打开文件句柄并逐行读取XML,直到达到$ max_B的阈值。如果文件太大,我们仍然需要读取直到10MB标记的开销,但它可以正常工作。如果文件小于$ max_B,它将继续...

Ok, finally working. The headers solution was obviously not going to work broadly. In this solution, we open a file handle and read the XML line by line until it hits the threshold of $max_B. If the file is too big, we still have the overhead of reading it up until the 10MB mark, but it's working as expected. If the file is less than $max_B, it proceeds...

$xml_file = "http://www.dailymotion.com/rss/user/dialhainaut/";
//$xml_file = "http://www.cs.washington.edu/research/xmldatasets/data/pir/psd7003.xml";

$fh = fopen($xml_file, "r");  

if($fh){
    $file_string = '';
    $total_B = 0;
    $max_B = 10485760;
    //run through lines of the file, concatenating them into a string
    while (!feof($fh)){
        if($line = fgets($fh)){
            $total_B += strlen($line);
            if($total_B < $max_B){
                $file_string .= $line;
            } else {
                break;
            }
        }
    } 

    if($total_B < $max_B){
        echo 'File ok. Total size = '.$total_B.' bytes. Proceeding...';
        //proceed
        $dom = new DOMDocument();
        $dom->loadXML($file_string); //NOTE the method change because we're loading from a string   

    } else {
        //reject
        echo 'File too big! Max size = '.$max_B.' bytes.';  
    }

    fclose($fh);

} else {
    echo '404 file not found!';
}

这篇关于如果文件大于给定大小,则阻止从远程源加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆