PHP read file instead of download - php

If a web page forces through the headers the download of a file. Example shown below!
trying_to_parse.php:
header('Content-disposition: attachment; filename="examplefile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('examplefile.xml');
Using php im attempting to grab and parse this file by:
example.php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'trying_to_parse.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);
echo $xml;
curl_close($ch);
This did not work because with curl their are no headers.
So i went ahead and tried a few more thing like fopen and file_get_... and these also did not work.
Any ideas on how to actually load a file that is being forced through the headers to download?
I want to use:
$dom->loadXML($xml);
But ill take anything :)
I want to parse the ForceDownloadedFile.xml file that is force downloaded when you visit a URL. Any idea?

That's a 301 redirect, so you need to tell curl to follow that redirect, and fetch the file from the new location:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Related

Manipulate MP4 video with curl

I am accessing a remote .mp4 video using curl. I can display it in the browser, but the functions do not work correctly. The total minutes of the video do not appear and I can not advance or rewind the video.
What is responsible for these functions? The header? How can I make it work in curl?
My current script is this:
<?php
header('Content-Type: video/mp4');
header('Content-Disposition: filename="video.mp4"');
$url = 'http://www.exemple.com/video.mp4';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_exec($ch);
curl_close($ch);
?>
You need to add the header
Content-Length
So the client knows how much of the data is remaining.
Also make sure your script acts as a stream, not that you read the whole file and print it back. Google for php output buffering. I don't think it causes your issue but it improves performance and server health tho.

php curl return downloaded content to user while file is still downloading

I want to server begins to download a big file. But while this file is downloading output the file content to the user. I tried this code:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 155000);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch); // get curl response
echo $response;
But this code takes a long time. I want to use curl instead of readfile.
See this answer: Manipulate a string that is 30 million characters long
Modifing the MyStream class should change it enough so that you can just echo the results to the browser. Assuming the browser is already downloading the file, it should just keep downloading it.

Error download file from subdomain in php?

I have a sample code:
download
=> result (download from www.demo.simple.com)
But when I using:
$url = 'www.demo.simple.com/file.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
header("Content-type: application/zip");
echo $result;
=> result download from www.simple.com
I don't know why using code php download a file from sub domain www.demo.simple.com is popup show info download from from www.simple.com
You may need to add the protocol otherwise the domain would appear to be a path under the current domain -> add http://
Try:
http://www.demo.simple.com/file.zip
Also, in order to force it to download as a file instead of displaying in page (such as downloading pdf rather than browser displaying the pdf), add the following header.
header('Content-Disposition: attachment; filename="file.zip"');

File download PHP script doesn't work because of server delay

I have this code to download a file, but on sourceforge.net sever there is a 5 seconds delay before file starts to download (You can see it if you try to load this link in browser). And I have file with zero size after script is done. How can I download this file? Thanx in advance!
$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Sourceforge uses a meta refresh tag to start the download and because CURLOPT_FOLLOWLOCATION responds to Location: header it will most likely not help.
I think you're going to have to do som HTML parsing to achieve what you want to do. You have to find this line:
<meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe?r=&ts=1333621946&use_mirror=switch">
Then you must get the url from the line and load that.
It's possible that Sourceforge uses some cookie or session based stopper for this kind of downloads so you may have to compensate for that.
I haven't tested this but it looks like this is close to the way you have to do this.
You can try this:
$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);
To avoid timeouts in PHP you can use:
set_time_limit($hugetimeout);
before your script. You can read further documentation here.
On the download page, there's a direct link, you could try using that instead ?

download a zip file from a url

I wanna use php to download some zip files from a url. I.E. sitename.com/path/to/file/id/123 when you go to this url directly you get a file download prompt. I've tried to use fopen() and file_get_contents() but these fail. The search I've done comeback with how to make a zip file downloadable or how to get the file from sitename.com/path/to/file.zip but my url doesn't have a .zip extension.
fopen ('url.com') or die('can not open');
The browser shows can not open
The url is probably a script that may be redirecting you. Use CURL instead
$fh = fopen('file.zip', 'w');
$ch = curl_init()
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
fclose($fh);
It's probably easier to use the more basic functions like readfile() for this sort of thing.
Adapted from an example at php.net:
<?php
// We'll be outputting a ZIP
header('Content-type: application/zip');
// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header('Content-Disposition: attachment; filename="downloaded.zip"');
// The ZIP source is in original.zip
readfile('original.zip');
?>

Categories