PHP:cURL下载ZIP文件-较小的文件大小
我正在尝试将一个ZIP文件从外部服务器下载到自己的服务器。PHP脚本如下所示:PHP:cURL下载ZIP文件-较小的文件大小,php,curl,download,zip,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_exis
<?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文件 你真的让事情变得更复杂了:
<?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);
?>
请参阅有关如何使用和的文档。您真的让这个问题变得比需要的更复杂了:
<?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);
?>
请参阅有关如何使用和的文档。它为什么应该工作?您打开了
$destination
,但将curl指向了$ziel
,这是一个未定义的变量。为什么它要工作?您打开了$destination
,但将curl指向$ziel
,这是一个未定义的变量。非常感谢。下载作品!同时也感谢您提供了指向文档的有用链接。在对“file\u get\u contents($URL)”进行了一点练习后,我发现这个命令并没有下载完整的文件,只是其中的一部分。要下载的ZIP文件大约为1.5MB,但该命令只下载1121字节(!)——有时更多,有时更少。下载的ZIP文件完全没有价值。那么还有其他选择吗?这可能是您下载的服务器的问题。但是,当我使用相同的URL到ZIP文件并将其放入浏览器地址字段时,我可以手动下载ZIP文件。因此,我下载的服务器是正常的。我添加了参数“FILE_USE_INCLUDE_PATH”并更改为“$filedata=FILE_get_contents($URL,FILE_USE_INCLUDE_PATH);”下载现在可以了。非常感谢。下载作品!同时也感谢您提供了指向文档的有用链接。在对“file\u get\u contents($URL)”进行了一点练习后,我发现这个命令并没有下载完整的文件,只是其中的一部分。要下载的ZIP文件大约为1.5MB,但该命令只下载1121字节(!)——有时更多,有时更少。下载的ZIP文件完全没有价值。那么还有其他选择吗?这可能是您下载的服务器的问题。但是,当我使用相同的URL到ZIP文件并将其放入浏览器地址字段时,我可以手动下载ZIP文件。因此,我下载的服务器是正常的。我添加了参数“FILE\u USE\u INCLUDE\u PATH”并更改为“$filedata=FILE\u get\u contents($URL,FILE\u USE\u INCLUDE\u PATH);”下载现在可以工作了。