PHP:cURL下载ZIP文件 - 较小的文件大小

时间:2016-10-24 21:43:45

标签: php curl download zip

我正在尝试从外部服务器下载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文件。

1 个答案:

答案 0 :(得分: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()的文档。