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')); } 
5
  • you only want to download ? or extract too ? Commented Jun 2, 2017 at 4:53
  • @Sindhuja try this http://www.web-development-blog.com/archives/php-download-file-script/ Commented Jun 2, 2017 at 4:54
  • @Touheed Khan i want to download zip file from url example 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. Commented Jun 2, 2017 at 5:15
  • Possible duplicate of Download File to server from URL Commented Jun 2, 2017 at 5:16
  • @Alexander , file_put_contents("Tmpfile.zip", fopen("someurl/file.zip", 'r')); yes, this code is working only if i give static url. Commented Jun 2, 2017 at 5:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

15 Comments

its not about file duplicate,
@SindhujaN just try both solutions and tell me output. keep error reporting on.
@SindhujaN its just a precaution to avoid any issues.
getting empty zip file
give me url you're requesting.
|
-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.