繁体   English   中英

在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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM