简体   繁体   中英

Bast way to move file from server to server in PHP

I have sites where stored some xml files, and I want to download to our server, we don't have ftp connection, so we can download through http. I always used file(url) is there any better way to download files through php

如果你可以通过http访问它们, file() (将文件读入数组)和file_get_contents() (将内容读入字符串)完全没问题,前提是包装器已启用。

Using CURL could also be a nice option:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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