0

its not about file duplicate,

// not working code , because i assign $zip_url dynamic

function downloadZipFile($dynamic_url, $filepath){
//echo $dynamic_url;exit;
$zip_url          = $dynamic_url;
$destination_path = $filepath;
file_put_contents($destination_path, fopen($zip_url, 'r'));
 } 

// working code but here i assign $zip_url static

function downloadZipFile($dynamic_url, $filepath){
//echo $dynamic_url;exit;
$zip_url          = "http://training.costaclick.net/WAWS_1_9/Catalog/4dd946a8-32e6-43b8-a592-6596a4509ec5-out.zip";
$destination_path = $filepath;
file_put_contents($destination_path, fopen($zip_url, 'r'));
 } 
Sindhuja N
  • 31
  • 1
  • 2
  • you only want to download ? or extract too ? – Touheed Khan Jun 02 '17 at 04:53
  • @Sindhuja try this `http://www.web-development-blog.com/archives/php-download-file-script/` – Lakhwinder Singh Jun 02 '17 at 04:54
  • @Touheed Khan i want to download zip file from url example http://testdata/Catalog/d1f87802-be88-4a9b-8765-82d854ec6cd4-out.zip yes , i can download this zip file using above code, if i give copy and assign to varible url. if i give dynamic its not working. – Sindhuja N Jun 02 '17 at 05:15
  • Possible duplicate of [Download File to server from URL](https://stackoverflow.com/questions/3938534/download-file-to-server-from-url) – Alexander Jun 02 '17 at 05:16
  • @Alexander , file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r')); yes, this code is working only if i give static url. – Sindhuja N Jun 02 '17 at 05:24

2 Answers2

2

Try this code :

$ch = curl_init();
$source = "http://someurl.com/afile.zip"; //$source = $dynamic_url
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/sub_folder/". uniqid(time(), true) .".zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);

$source can be dynamic value of url. uniqid(time(), true) will generate random file name. In the we will store it in path specified in $destination variable.

Alternate Solution :

$zip_url          = "http://www.colorado.edu/conflict/peace/download/peace.zip";
$destination_path = "/var/www/html/files/".uniqid(time(), true)."zip";
file_put_contents($destination_path, fopen($zip_url, 'r'));

$zip_url will be dynamic zip url and $destination_path will be location on your local machine.

Note : make sure you've proper permission on destination path folder.

Touheed Khan
  • 2,149
  • 16
  • 26
-1
<?php // HTTP Headers for ZIP File Downloads
  // https://perishablepress.com/press/2010/11/17/http-headers-file-downloads/

  // set example variables
  $filename = "Inferno.zip";
  $filepath = "/var/www/domain/httpdocs/download/path/";

  // http headers for zip downloads
  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"".$filename."\"");
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: ".filesize($filepath.$filename));
  ob_end_flush();
  @readfile($filepath.$filename);
?>

reference link

ImranGalib
  • 114
  • 6