PHP脚本没有从URL下载压缩的产品源 - 下载在浏览器上访问时工作正常
I have a simple bit of PHP code which copies a zip file from a remote url to the server, and then extracts it into another folder.
function extract_remote_zip($new_file_loc, $tmp_file_loc, $zip_url) {
echo 'Copying Zip to local....<br>';
//copy file to local
if (!copy($zip_url, $tmp_file_loc)) {
echo "failed to copy zip from".$zip_url."...";
}
//unzip
$zip = new ZipArchive;
$res = $zip->open($tmp_file_loc);
if ($res === TRUE) {
echo 'Extracting Zip....<br>';
if(! $zip->extractTo($new_file_loc)){
echo 'Couldnt extract!<br>';
}
$zip->close();
echo 'Deleting local copy....<br>';
unlink($tmp_file_loc);
return 1;
} else {
echo 'Failed to open tmp zip!<br>';
return 0;
}
}
It works perfectly with one URL from Awin and downloads and extracts the correct 600kb zip, but with another from Webgains it just downloads a Zip file with size 0 bytes. I'm guessing the download is getting corrupted somewhere?
When I visit the URL on my browser it downloads the zip perfectly (the size is about 3mb). I just can't get it to download with PHP.
Please help!
</div>
我有一段简单的PHP代码,它将zip文件从远程URL复制到服务器,然后提取 它进入另一个文件夹。 p>
p>
function extract_remote_zip($ new_file_loc ,$ tmp_file_loc,$ zip_url){
echo'将Zip复制到本地....&lt; br&gt;';
//将文件复制到本地
if(!copy($ zip_url,$ tmp_file_loc)){
echo“无法复制zip”。$ zip_url。“...”;
}
// unzip
$ zip = new ZipArchive;
$ res = $ zip-&gt; open($ tmp_file_loc);
if($ res === TRUE){
\ n echo'提取Zip ....&lt; br&gt;';
if(!$ zip-&gt; extractTo($ new_file_loc)){
echo'Forent extract!&lt; br&gt;'; \ r
}
$ zip-&gt; close();
echo'删除本地副本....&lt; br&gt;';
unlink($ tmp_file_loc);
返回1;
}否则 {
echo'无法打开tmp zip!&lt; br&gt;';
返回0;
}
} code> pre>
div>
div>
它与Awin的一个URL完美配合,下载并提取正确的600kb zip,但是另一个来自Webgains,它只下载一个Zip文件 大小为0字节。 我猜测下载在某处被破坏了? p>
当我在浏览器上访问URL时,它会完美地下载zip(大小约为3mb)。 我只是无法用PHP下载它。 p>
请帮助! p>
div>
Since you didn't provide the problem URL, I can't say for sure, but you are likely encountering an issue with the method copy uses to read the file. Doing a direct curl call should resolve this.
Try the below:
function file_get_contents_curl( $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
$data = curl_exec( $ch );
if ( curl_errno( $ch ) <> FALSE ) {
echo "ERROR at line " . __LINE__ . " in file_get_contents_curl: error number: " . curl_errno( $ch ) . ' error : ' . curl_error( $ch ) . " url: $url";
return FALSE;
}
curl_close( $ch );
return $data;
}
function extract_remote_zip($new_file_loc, $tmp_file_loc, $zip_url) {
echo 'Copying Zip to local....<br>';
// read the zip
if ( $zip_str = file_get_contents_curl( $zip_url ) ) {
// write the zip to local
if ( !file_put_contents( $tmp_file_loc, $zip_str ) ) {
echo "failed to write the zip to: " . $zip_url;
return FALSE;
}
} else {
echo "failed to read the zip from: " . $zip_url;
return FALSE;
}
//unzip
$zip = new ZipArchive;
$res = $zip->open($tmp_file_loc);
if ($res === TRUE) {
echo 'Extracting Zip....<br>';
if(! $zip->extractTo($new_file_loc)){
echo 'Couldnt extract!<br>';
}
$zip->close();
echo 'Deleting local copy....<br>';
unlink($tmp_file_loc);
return 1;
} else {
echo 'Failed to open tmp zip!<br>';
return 0;
}
}