cURL下载ZIP文件-更小的文件大小


PHP: cURL download ZIP file - smaller filesize

我试图从外部服务器下载ZIP-File到自己的服务器。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); 
...
?>

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

在destination-Server上,我得到的文件比预期的小。较小的ZIP-File无法打开。

你把事情弄得太复杂了:

<?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()的使用文档