使用php从远程服务器获取文件并将其复制到本地服务器的最佳方法


Best way to get a file from remote server and copy to local server using php

假设远程服务器上有一个文件可以不受任何限制地下载,例如:您可以在浏览器中直接链接到该文件,它就会下载该文件,例如http://www.remotesite.com/video.avi将提示浏览器下载该文件。使用php时,获取文件并将其上传到本地服务器的最佳方式是什么,而无需将文件下载到我的PC,这是phpBB的情况,如果你在文件上传表单中放入url ?所需要的代码的一个例子也将是赞赏。由于

就用copy

$source = "http://www.remotesite.com/video.avi";
$dest = "video.avi";
copy($source, $dest);
$remote_file_contents = file_get_contents('http://remote_url/file/with.extension');
//Get the contents
$local_file_path = 'your/local/path/to/the/file/with.extension';
file_put_contents($local_file_path, $remote_file_contents);
//save the contents of the remote file

无需浏览器下载即可读写文件

<?php 
$file = 'http://www.remotesite.com/video.avi';
// read the file from remote location
$current = file_get_contents($file);
// create new file name
$name = "path/to/folder/newname.avi";
// Write the contents back to the file
file_put_contents($file, $current);