im trying to download a file from a url,
when I use the browser the download dialog works but when I use this code the new file on my server stay empty.
$ch = curl_init();
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=$row['certNo']&weight=$row['carat']";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "./files/certs/$row['certNo'].pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
example of url: https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35
I solved this problem using this:
curl_setopt($ch, CURLOPT_SSLVERSION,3);
This is the final code:
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
$destination = "./files/test.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
Your url using https connection. Use the following curl option as well.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
Did you check if result actually contains data?
with fputs on larger files you need to specify byte length
Try using file_put_contents($destination, $data);
See if you can use the following code to help you.
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);
Related
I am using this script to upload myfile by curl function and ftp connection.
In local it works fine but in my server file is appeared uploaded but it have zero file size.
what is wrong? Thank You.
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
and also how can I upload multiple files in this script like:
$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip');
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip');
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip');
This source may helps your problem.
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/
To upload the file in curl you can use the curl_file_create.Try the below one:
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
So I want to download a mp3 file from a dynamic link, when I run the script it creates test.mp3 but it's empty.
Code :
$source = 'http://www.youtubeinmp3.com/download/get/?
i=yt6MfhzTljRBZ6AjqSsnb2ih1ZifuMoY&e=69';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSLVERSION,0);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
$destination = "test.mp3";
$file = fopen($destination, "w+");
fwrite($file, $data);
fclose($file);
There is nothing in the error.log
EDIT
If I add :
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
It downloads the source of the page (HTML) but not the mp3 file.
Because You are not downloading anything.
Check output of your $data = curl_exec ($ch);. It must be empty
I have an url with an image: http://www.example.com/image.jpg
and I have an action in the controller that uploads a file to the server by user input. I want to use this same action to upload a file by url. I started to get the data from image:
$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
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);
Now how do I send the $rawdata to the controller to be handled like a normal user upload?
Thanks
I figured it out myself.
$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;
$ch = curl_init($url);
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);
if(file_exists($path)){
unlink($path);
}
$file = fopen($path, 'x');
fwrite($file, $rawdata);
fclose($file);
$post_data['Filedata'] = '#'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
echo $response;
In the controller:
function action(){
//get properties of the file
//$_FILES['Filedata']['name'];
//$_FILES['Filedata']['size'];
//$_FILES['Filedata']['tmp_name'];
//savefile();
}
If anyone knows how to post the file without saving it will be great.
Trying to save image to my server using cURL. The image appears to download. It shows the correct bytes but when i link image does not work. I then DL to see and nope its a blank image.
here is my code... whats the issue with it?
$ch = curl_init("'. $image .'");
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("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
I test your script it work fine for me, just remove the useless double quote and dot for $image.
<?
$image ="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5";
$rename="123";
$ch = curl_init($image);
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("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>
How do I download an external image off a url with cURL?
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
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);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
http://www.bitrepository.com/download-image.html
http://php.bigresource.com/Track/php-Jjg3DsKY/
from php.net - also reads into a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>