Let's say I want XML Files only with upto 10MB to be loaded from a remote server.
Something like
$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.
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/"
)
);
I get string 'unknown' (length=7)
When I try with get_headers
as suggested below, the Content-Length is missing in the headers, so this will not work reliably either.
Please kindly advise how to determine the length
and avoid sending it to the DOMDocument
if it exceeds 10MB
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...
10MB is equal to 10485760 B. If content-length is not specified, it will use curl which is available since php5. I got this source from somewhere in SO but could not remember it.:
.
Check demo here
Edit: New Answer a bit workaroundish:
You can't check the Dom Elements Length, BUT, you can make a header request and get the filesize from the URL:
You should now be able to get every Filesize you want by URL just with