php 从远程服务器或 URL 复制文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9843933/
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
Copy file from remote server or URL
提问by Kris
I have the following code:
我有以下代码:
$file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg';
$newfile = '/img/submitted/yoyo.jpg';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
and it always output "Copy failed"
它总是输出“复制失败”
copy(/img/submitted/yoyo.jpg) [function.copy]: failed to open stream: No such file or directory
my directory is set to 777.
我的目录设置为 777。
any ideas? thanks!
有任何想法吗?谢谢!
回答by Mark Biek
While copy()
will accept a URL as the sourceargument, it may be having issues a url for the destination.
虽然copy()
将接受一个 URL 作为源参数,但它可能会遇到目标url 的问题。
Have you tried specifying the full filesystem path to the output file? I'm assuming you're not trying to put the new file onto a remote server.
您是否尝试过指定输出文件的完整文件系统路径?我假设您没有尝试将新文件放到远程服务器上。
For example:
例如:
$file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/img/submitted/yoyo.jpg';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
The above worked nicely for me.
以上对我来说效果很好。
回答by Athiwat Chunlakhan
I found this function in one of my old project.
我在我的一个旧项目中发现了这个功能。
private function download_file ($url, $path) {
$newfilename = $path;
$file = fopen ($url, "rb");
if ($file) {
$newfile = fopen ($newfilename, "wb");
if ($newfile)
while(!feof($file)) {
fwrite($newfile, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newfile) {
fclose($newfile);
}
}
回答by Starx
If the file is not publically accessible then you cannot copy a file from a server without having access to it.
如果该文件不可公开访问,则您无法在无法访问的情况下从服务器复制文件。
You can use ftp_get()to open up a FTP connection and copy file.
您可以使用ftp_get()打开 FTP 连接并复制文件。
$local_file = 'localname.zip'; // the nam
$server_file = 'servername.zip';
$conn = ftp_connect($ftp_server);
$login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
if (ftp_get($conn, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully copied";
}
ftp_close($conn);
But, If you want to download a file from URL
但是,如果您想从 URL 下载文件
$fullPath = "filepath.pdf";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);