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);
?>
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);
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.
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);
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
?>
download file with curl via sftp
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$d = curl_exec($curl);
// data is in $d var, write it somewhere ;)
$local = "/tmp/myfile.txt";
file_put_contents($local, $d);
Related Topics
Variable Variables in PHP - What Is Their Purpose
How to Call Entity Manager in a Constructor
Yii2 Links Between Frontend and Backend (Advanced Template)
How to Send a File via Axios to Laravel
How to Resolve the Error: SQL Authentication Method Unknown in Laravel-Mysql
How to Launch a PHP Script at Apache Startup
Elegant Way to Search an PHP Array Using a User-Defined Function
How to Set Base Url for All Pages of My Website
Expected Response Code 250 But Got Code "", with Message ""
Model->Save() Not Working in Yii2
Filter_Sanitize VS Filter Validate, Whats the Difference - and Which to Use
Selecting Rows Where a Field Is Null Using PHP Pdo Prepared Statements and MySQL
Php's Simplexml: How to Use Colons in Names
PHP Foreach() with Arrays Within Arrays