PHP 脚本未从 URL 下载压缩的产品提要 - 在浏览器上访问时下载工作正常
PHP Script Not Downloading A Zipped Product Feed From URL - Download works fine when visited on browser
我有一段简单的 PHP 代码,可以将 zip 文件从远程 url 复制到服务器,然后将其解压缩到另一个文件夹中。
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;
}
}
它与来自 Awin 的一个 URL 完美配合并下载并提取正确的 600kb zip,但与另一个来自 Webgains 的它只下载一个大小为 0 字节的 Zip 文件。我猜下载在某处损坏了?
当我在浏览器上访问 URL 时,它会完美下载 zip(大小约为 3mb)。我无法通过 PHP.
下载它
请帮忙!
由于您没有提供问题 URL,我不能肯定地说,但您可能遇到了复制用于读取文件的方法的问题。直接调用 curl 应该可以解决这个问题。
试试下面的方法:
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;
}
}
我有一段简单的 PHP 代码,可以将 zip 文件从远程 url 复制到服务器,然后将其解压缩到另一个文件夹中。
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;
}
}
它与来自 Awin 的一个 URL 完美配合并下载并提取正确的 600kb zip,但与另一个来自 Webgains 的它只下载一个大小为 0 字节的 Zip 文件。我猜下载在某处损坏了?
当我在浏览器上访问 URL 时,它会完美下载 zip(大小约为 3mb)。我无法通过 PHP.
下载它请帮忙!
由于您没有提供问题 URL,我不能肯定地说,但您可能遇到了复制用于读取文件的方法的问题。直接调用 curl 应该可以解决这个问题。
试试下面的方法:
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;
}
}