【发布时间】: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,这是一个未定义的变量。