在PHP中从服务器到服务器移动文件的最快捷方式


Bast way to move file from server to server in PHP

我有一些站点存储了一些xml文件,我想下载到我们的服务器,我们没有ftp连接,所以我们可以通过http下载。我一直用file(url)有没有更好的方法通过php下载文件

如果您可以通过http访问它们,file()(将文件读入数组)和file_get_contents()(将内容读入字符串)在启用包装器的情况下是完全可以的。

使用CURL也是一个不错的选择:

// create a new CURL resource 
$ch = curl_init(); 
// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "http://www.server.com/file.zip"); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
set_time_limit(300); # 5 minutes for PHP 
curl_setopt($ch, CURLOPT_TIMEOUT, 300); # and also for CURL 
$outfile = fopen('/mysite/file.zip', 'wb'); 
curl_setopt($ch, CURLOPT_FILE, $outfile); 
// grab file from URL 
curl_exec($ch); 
fclose($outfile); 
// close CURL resource, and free up system resources 
curl_close($ch);