从远程服务器获取文件并使用 php 复制到本地服务器的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16518033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get a file from remote server and copy to local server using php
提问by Jizbo Jonez
Let's say there is a file on a remote server that can be downloaded without any restrictions, ie. you can put the direct link to the file in your browser and it downloads the file, for example http://www.remotesite.com/video.aviwill prompt your browser to download that file. Using php, what is the best way to grab that file and upload it to my local server without the file being downloaded to my PC at all, which is what happens with phpBB if you put a url in the file upload form? An example of the code needed would also be appreciated. Thanks
假设远程服务器上有一个文件可以不受任何限制地下载,即。您可以将文件的直接链接放在浏览器中,它会下载文件,例如http://www.remotesite.com/video.avi将提示您的浏览器下载该文件。使用 php,获取该文件并将其上传到我的本地服务器的最佳方法是什么,而无需将文件下载到我的 PC,如果在文件上传表单中放置一个 url,phpBB 会发生什么?所需代码的示例也将不胜感激。谢谢
回答by Baba
Just use copy
只需使用 copy
$source = "http://www.remotesite.com/video.avi";
$dest = "video.avi";
copy($source, $dest);
回答by Elavarasan M Lee
$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
回答by Sundar
You can read and write the file without browser download
无需浏览器下载即可读写文件
<?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);