Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从url下载zip文件_Php - Fatal编程技术网

使用php从url下载zip文件

使用php从url下载zip文件,php,Php,这与文件复制无关 //不工作的代码,因为我分配$zip_url动态 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')); } //工作代码,但在

这与文件复制无关

//不工作的代码,因为我分配$zip_url动态

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'));
 } 
//工作代码,但在这里我分配$zip\uURL静态

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'));
 } 

尝试以下代码:

$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);
$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'));
$source
可以是url的动态值
uniqid(time(),true)
将生成随机文件名。在中,我们将它存储在
$destination
变量中指定的路径中

替代解决方案:

$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);
$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\u url
将是动态zip url,
$destination\u path
将是本地计算机上的位置

注意:确保您对目标路径文件夹拥有正确的权限。


<?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);
?>

您只想下载吗?还是也提取?@Sindhuja试试这个
http://www.web-development-blog.com/archives/php-download-file-script/
@Touheed Khan我想从url下载zip文件示例是的,如果我提供副本并分配给可变url,我可以使用上述代码下载此zip文件。如果我给dynamic它不工作。可能重复@Alexander,文件内容(“Tmpfile.zip”,fopen(“,'r'));是的,这段代码只有在我给出静态url时才起作用。这与文件重复无关,@SindhujaN请尝试这两种解决方案并告诉我输出。继续报告错误。@SindhujaN这只是一种预防措施,以避免任何问题。获取空的zip文件给我您请求的url。