【问题标题】:PHP: cURL download ZIP file - smaller filesizePHP:cURL 下载 ZIP 文件 - 较小的文件大小
【发布时间】:2016-10-24 21:43:45
【问题描述】:

我正在尝试将 ZIP 文件从外部服务器下载到自己的服务器。 PHP脚本如下:

<?php    
...
# Some URL
$URL = 'https://xyz.source.com/Path/where/ZIP-Files/file.zip'; 
# Destination
$destination = fopen("../path/to/file.zip","w+");   

# Check, if cURL is installed: 
if (!function_exists('curl_init')){ 
    die('cURL it not installed!'); 
}

# New cURL Session
$ch = curl_init();

# Set Options 
# Set Download-URL
curl_setopt($ch, CURLOPT_URL, $URL);
# unknown about Header 
# curl_setopt($ch, CURLOPT_HEADER, 0);

# Set to "false", but I don't know why due to lack of explanation. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);

# Set to "true", but I don't know why, due to lack of explanation.
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

curl_setopt($ch, CURLOPT_FILE, $ziel);

# curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# curl_setopt($ch, CURLOPT_TIMEOUT, 1000); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');

# Execution 
curl_exec($ch);

# Check for errors
if(curl_exec($ch) === false) {
     echo 'Curl-Error: ' . curl_error($ch);
} else {
    echo 'cURL - no problems';
} 

# end of cURL-Session
curl_close($ch);


fclose($destination); 
...
?>

发生了什么事? - 有一个文件,但更小:

在目标服务器上,我得到的文件比预期的要小。无法打开较小的 ZIP 文件。

【问题讨论】:

  • 为什么它应该起作用?你打开$destination,但curl指向$ziel,这是一个未定义的变量。

标签: php curl download zip


【解决方案1】:

你真的让这变得比它需要的更复杂:

<?php    
...
# Some URL
$URL = 'https://xyz.source.com/Path/where/ZIP-Files/file.zip'; 
# Destination
$destination = "../path/to/file.zip";

$filedata = file_get_contents($URL);
file_put_contents($destination, $filedata);
?>

请参阅有关如何使用 file_get_contents()file_put_contents() 的文档。

【讨论】:

  • 非常感谢。下载作品!也感谢文档的有用链接。
  • 在对“file_get_contents($URL)”进行了一些尝试之后,我发现这个命令下载了完整的文件,只是其中的一部分。要下载的 ZIP 文件大约为 1.5 MB,但该命令仅下载 1121 字节 (!) - 有时更多,有时更少。下载的 ZIP 文件完全没有价值。那么还有其他选择吗?
  • 那是你下载的服务器的问题。
  • 但是当我使用 ZIP 文件的相同 URL 并将其放入浏览器地址字段时,我可以手动下载 ZIP 文件。因此,我从中下载的服务器是可以的。
  • 我已添加参数“FILE_USE_INCLUDE_PATH”并更改为“$filedata = file_get_contents($URL, FILE_USE_INCLUDE_PATH);”现在可以下载了。
猜你喜欢
  • 1970-01-01
  • 2019-01-20
  • 2012-06-06
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
相关资源
最近更新 更多