Error download file from subdomain in php? - 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"');

Related

Php download file from link that downloads file

I'm trying to use php to download files that are created and downloaded when you call to a url.
In this case i'm trying to programmatically download a txt file from the dutch weather institute wich can be done by going to(in this example) this url http://projects.knmi.nl/klimatologie/daggegevens/getdata_dag.cgi?lang=nl&byear=2016&bmonth=6&bday=20&eyear=2016&emonth=6&eday=22&variabele=FHX&variabele=FXX&variabele=TG&variabele=TN&variabele=TX&stations=249&submit=Download+data+set
Going to that url downloads the file.
I want to do this with php so I can use the contents of the file to make custom charts.
Does anyone know how to do this with php?
Using file_get_contents(): http://php.net/manual/en/function.file-get-contents.php
or using CURL functions: http://php.net/manual/en/function.curl-exec.php
EDIT: CURL Solution:
$url = 'http://projects.knmi.nl/klimatologie/daggegevens/getdata_dag.cgi?lang=nl&byear=2016&bmonth=6&bday=20&eyear=2016&emonth=6&eday=22&variabele=FHX&variabele=FXX&variabele=TG&variabele=TN&variabele=TX&stations=249&submit=Download+data+set';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$str = curl_exec($curl);
Try:
$content = file_get_contents("The URL here");
echo $content;

php how can get and download image from feelgrafix

i want to download image from this url
http://feelgrafix.com/959413-rococo.html
and this the source image
http://feelgrafix.com/data_images/out/28/959413-rococo.jpg
but when i download image from this source
file Download page url not download source image
this the code i used it
$url = 'http://feelgrafix.com/data_images/out/28/959413-rococo.jpg';
$ch = curl_init($imgURL);
$fp = fopen('image.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
and this Other way
$content = file_get_contents($url); file_put_contents('sadsdasd.jpg',$content);
i think that this the protect from server ..
no one can download OR see the image direct before open the page home
so what can i do ?
Instead of using curl function i recommend you to user file_get_content($url) to fetch the file and file_put_contents($path) to save it in your desired path and in case if any error it throws just usin # before both the function.

cURL script will not download images; Instead renders junk

I have been using the following function to download pictures from a distributor for use on our website as was described here:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$itemnum = 80848;
$path = "www.gullions.com/localstore/test/test.jpg";
header('Content-type: image/jpeg');
$ch = curl_init($url);
$fp = fopen("http://www.gullions.com/localstore/test/test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Instead of downloading and saving the picture, it only prints crazy characters to the screen. I know that means that for some reason the browser is trying to render the picture but most likely doesn't recognize the file type. But I can't seem to find the problem. You can see the crazy output by navigating here. To verify that the image wasn't downloaded and saved, you can navigate here. Also, FTP also shows that no file was downloaded. If you navigate to the original picture's download url you'll see that the file we are trying to download does in fact exist.
I have contacted my host and verified that no settings have been changed with the server, that cURL is functioning properly, and have even rolled back my browser to verify that a recent update didn't cause the issue. I created a test file by removing the function from the application and have tried running it separately but have only had the same results.
Add:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
as by default this parameter is false. Docs read:
TRUE to return the transfer as a string of the return value of
curl_exec() instead of outputting it out directly.
EDIT
Setting CURLOPT_RETURNTRANSFER make curl_exec() return data, so it should be written manually, like this:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
fwrite($fp, curl_exec($ch));
curl_close($ch);
fclose($fp);
Also this code, that uses CURLOPT_FILE works for me just fine:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
so I basically suspect that your file handle is not valid, therefore cURL falls back to default output. Try this code with elementary error checking (you should ALWAYS check for errors):
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$fp = fopen("./test.jpg", 'wb');
if( $fp != null ) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
} else {
exit('ERROR: Failed to open file');
}
Note that my examples write to the same folder scripts sits in. It must work unless your server got file permissions messed badly. If it works for you, then investigate if (usually) user your scripts runs as can write to your target folder.
You haven't told your browser what type of file to expect, so it's probably defaulting to text/plain.
You need at least:
header('Content-type: image/jpeg');
As well, curl by default outputs whatever it fetches, unless you explicitly tell it you want to have the fetched data returned, or saved directly to file.

PHP read file instead of download

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);

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