第一次尝试:
我用过这段代码:(感谢user580950的回答)
// define some variables
$local_file = 'archive.tar';
$server_file = '/path/to/archive.tar';
$ftp_server = "1.2.3.4";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$port_number = 123
$conn_id = ftp_connect($ftp_server, $port_number);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
}
else {
echo "There was a problem\n";
}
// close the connection
ftp_close($conn_id);
存档是一个大文件,少于2GB
.
但几秒钟后,我看到了There was a problem
。
第二次尝试:
$url = 'http://site.com/archive.tar';
file_put_contents("archive.tar", fopen($url, 'r'));
第三次尝试:
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);
第四次尝试:
$fp = fopen (dirname(__FILE__) . '/archive.tar', 'w+');//This is the file where we save the information
$ch = curl_init($url);//Here is the file we are downloading, replace spaces with %20
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
第五个:
function download($file_source, $file_target) {
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'w+b');
if (!$rh || !$wh) {
return false;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 4096)) === FALSE) {
return false;
}
echo ' ';
flush();
}
fclose($rh);
fclose($wh);
return true;
}
set_time_limit(0);
var_dump(download($url, 'archive.tar')); // Returns bool(false)
我应该感谢 SO 中的许多人使用他们的代码,但仍然没有运气。