PHP Curl download File with filename - php

I have a Curl Download File but The problems I have it's I need to output files with the filename where is output:
example:
$result = false;
$path = "http://mywebsite.com/download.php?file=123";
set_time_limit(0); //prevent timeout
$ch2=curl_init();
$file = fopen('upload/'.$name,'w+');
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_FILE, $file); //auto write to file
curl_setopt($ch2, CURLOPT_TIMEOUT, 5040);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_SSLVERSION, 3);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch2);
curl_close($ch2);
fclose($file);
In my example, the I need the name of the download file is $name = (the file name)
like if I download file I can return custom_name_file.zip
but I need the writing file will the name of this custom_name_file
any idea?

Related

Downloaded pdf file using curl - issue

This is my curl code:
$fp = fopen('path/to/file/'. $id . '.pdf', 'w+');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'myurl');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, '');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_exec($curl);
I managed to download my file. I can open it , but not in Chrome. Plus, if I am trying to open it with $pdf = new Zend_Pdf(file_get_contents($pdfFile)); I am receiving the following error: File is not a PDF
I believe the way how I am saving under a specific name and put it on a specific folder, is not correct. Any way, ideas how to approach this ?
Thank you
When you are trying to retrieve "structured" files such as a PDF you do not want the headers returned too as they will usually corrupt the file being retrieved. Change
curl_setopt($curl, CURLOPT_HEADER, 1);
to
curl_setopt($curl, CURLOPT_HEADER, 0);

Download csv file from server using curl in php

I am struck with this. I want to download a csv file from url using curl. I have referred all the answer in stackoverflow and tried all. But not getting what i am expected. i have the following code.
define("COOKIE_FILE", "cookie.txt");
$path = "settlement_file/test.csv";
set_time_limit(0);
$fp = fopen ($path, 'w+');//This is the file where we save the information
$ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
You have to remove the curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); statement. It makes curl_exec return the data instead of writing it to a file. Since it comes after the curl_setopt($ch, CURLOPT_FILE, $fp); it overrides that, so just remove the former line.

php code saving a file in hosting server

when i use below code to save an image it saves perfectly
its a part of a script..and i face only problem here
i think its due to proxy use..
bt i have to use proxy in this script so cant remove that
bt plz need a correct answer
here is without proxy part of script
if(preg_match_all('/unsanswerit.php(.*?)"/i',$html, $matches))
{
$img="".$url."/unsanswerit.php".$matches[1][3]."";
curl_setopt($ch, CURLOPT_URL, $img);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$abc=curl_exec($ch);
$name=rand(0, 999999);
$path='img/'.$name.'.png';
$fh = fopen($path, "x");
fwrite($fh, $abc);
fclose($fh);
$st=file_get_contents($path);
$image=imagecreatefromstring($st);
$anf= img2txt($image);
imagedestroy($image);
}
and here is part of script with proxy
if(preg_match_all('/unsanswerit.php(.*?)"/i',$html, $matches))
{
$img="".$url."/unsanswerit.php".$matches[1][3]."";
curl_setopt($ch, CURLOPT_URL, $img);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$abc=curl_exec($ch);
$name=rand(0, 999999);
$path='img/'.$name.'.png';
$fh = fopen($path, "x");
fwrite($fh, $abc);
fclose($fh);
$st=file_get_contents($path);
$image=imagecreatefromstring($st);
$anf= img2txt($image);
imagedestroy($image);
}
ok guys i have found the solution
its header problem
header should be false when saving image via curl exe

How to download a zip file and extract and parse xml file in it using PHP cURL

I have a script that usually download xml from an external server with a class.
Now I have with the same code download a zip file, unzip it and this is the xml to parse after.
My initial problem is how to retrieve a zip file unzip and take the xml?
This is my actual code:
$ch2=curl_init();
curl_setopt($ch2, CURLOPT_URL, $this->URL);
curl_setopt($ch2, CURLOPT_TIMEOUT, 540);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS,$this->XMLRequest);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_SSLVERSION, 3);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
$httpHeader2 = array(
"Content-Type: text/xml; charset=UTF-8",
"Content-Encoding: UTF-8"
);
$zip = curl_exec($ch2);
$this->errno=curl_getinfo( $ch2, CURLINFO_HTTP_CODE );
curl_close($ch2);
How to get the zip file and unzip it to retrieve the xml inside?
<?php
//Add this to end of your code
file_put_content('./temp.zip' ,$zip );
$ZipArchive = new ZipArchive;
if ($ZipArchive->open('./temp.zip' ) === TRUE) {
$ZipArchive->extractTo('/my/destination/dir/');
$ZipArchive->close();
echo 'ok';
} else {
echo 'failed';
}
?>

how to download images to a folder in php

I want to download a image from the url but it is not getting downloaded.Please check out the code and tell me where i am going wrong.
<?php
$ch = curl_init ("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("img.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>
It's working fine, set proper file permissions to where the image should be saved. In this case it's the same folder where your script is, might want to move it somewhere else like:
// where "images" folder can be written with files
// set permissions to 0755
$fp = fopen("images/img.jpg",'w');
function download_image ($url, $the_filename) {
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Debian) Firefox/0.10.1");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLINFO_EFFECTIVE_URL, true);
curl_setopt($ch, CURLOPT_REFERER, "http://tomakefast.com");
curl_setopt($ch, CURLOPT_POST, false);
$rawdata=curl_exec($ch);
curl_close($ch);
file_put_contents("../somewhere/". $the_filename, $rawdata);
}
If you see any problems, please let me know. This occasionally gives me a 0 byte image file but that could happen for any number of reasons. This could be improved to return false on 0 bytes, maybe even do a basic test to see if indeed an image was downloaded.
you can use this simple code instead if you feel this is better.
$img_file = file_get_contents("http://l1.yimg.com/t/frontpage/cannes_anjelina_60.jpg");
file_put_contents("myImage.jpg", $img_file);

Categories