Downloading a large file using curl
<?php
set_time_limit(0);
//This is the file where we save the information
$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
// make sure to set timeout to a high enough value
// if this is too low the download will be interrupted
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Download file from URL using CURL
Give this a go
<?php
$output_filename = "testfile.igc";
$host = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
print_r($result); // prints the contents of the collected file before writing..
// the following lines write the contents to a file in the same directory (provided permissions etc)
$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);
?>
#or if you want to put it within a loop for parsing several links... you need some functions.. here is a rough idea....
<?php
function collect_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return($result);
}
function write_to_file($text,$new_filename){
$fp = fopen($new_filename, 'w');
fwrite($fp, $text);
fclose($fp);
}
// start loop here
$new_file_name = "testfile.igc";
$url = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";
$temp_file_contents = collect_file($url);
write_to_file($temp_file_contents,$new_file_name)
// end loop here
?>
PHP CURL Download File
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);
Downloading Files with PHP and CURL
with the CURLOPT_RETURNTRANSFER
option is set curl_exec
will return downloaded content in the result. So, your variable $result
actually contains the content of the file (PKr�
... is a ZIP-header)
So, all you need, just to save the content of the variable in a file, instead of echo
ing it into the browser.
For example.
...
$result = curl_exec($ch);
file_put_contents('downloaded.zip', $result); // save the string to a file
curl_close ($ch);
How to download and save a file to local path using CURL
Ok, got the solution. Sharing my answer.
$getFile = 'http://url to file/file.csv';
$getParams = array ();
$path = '/var/save/to/local/path/file_name.csv';
$fp = fopen($path, 'w');
$ch = curl_init($getFile);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies');
$data = curl_exec($ch);
if(fwrite($fp,$data))
{
return true;
}
else
{
return false;
}
Thanks all for your help.
Reference :
http://www.technologyworkshops.net/php/how-to-download-and-save-a-file-to-local-path-using-curl-t132.html
php curl to download files
If i interpret this correct you want to build something like a cache where you download file x once from a remote server and after that the file should be served from your own server ?
If that is the case this could be a good approach for the problem:
class Downloader
{
private $path;
public $filename;
public function __construct()
{
$this->path = dirname(__FILE__);
}
public function getFile($url)
{
$fileName = $this->getFileName($url);
if(!file_exists($this->path."/".$fileName)){
$this->downloadFile($url,$fileName);
}
return file_get_contents($this->path."/".$fileName);
}
public function getFileName($url)
{
$slugs = explode("/", $url);
$this->filename = $slugs[count($slugs)-1];
return $this->filename;
}
private function downloadFile($url,$fileName)
{
//This is the file where we save the information
$fp = fopen ($this->path.'/'.$fileName, 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
}
$downloader = new Downloader();
$content = $downloader->getFile("YOUR URL");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$downloader->filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . mb_strlen($content));
print $content;
exit;
This is a very basic concept there are many point this have to be improved and customized for your use case.
Related Topics
Why Is My Number Value Changing Using Number_Format()
PHP Array_Intersect() Efficiency
Can a Client View Server-Side PHP Source Code
How to Rename a Tag in Simplexml Through a Dom Object
Fatal Error: Call to Undefined Function Oci_Connect()
Add a Line Break in Woocommerce Product Titles
Guzzle 6: No More JSON() Method for Responses
How to Overload Class Constructor Within Traits in PHP >= 5.4
Using Scandir() to Find Folders in a Directory (Php)
How to Create Clean Url Using .Htaccess
Parse Error: Syntax Error, Unexpected '' (T_Encapsed_And_Whitespace)
How to Add Two Strings Together
Php/Pdo/Mysql: Inserting into Mediumblob Stores Bad Data
How to Get the File Path in HTML <Input Type="File"> in PHP
Trying to Replace Parts of String Start with Same Search Chars