使用PHP将映像从一台服务器复制到另一台服务器

Dav*_*vid 3 php copy image

我正在尝试使用以下代码从远程服务器复制图像:

$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/";
file_put_contents($dest, file_get_contents($src));
Run Code Online (Sandbox Code Playgroud)

不幸的是我一直收到以下错误:

警告:file_put_contents(/ server/location/upload /)[function.file-put-contents]:无法打开流:第220行/server/location/myscript.php中的目录

你有什么想法如何解决这个问题?

nac*_*ito 9

$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
Run Code Online (Sandbox Code Playgroud)

您需要指定文件名.我添加了basename($src)哪个将写入与原始文件相同的文件名.如果你从其他目录复制,要小心,basename()只返回文件名,所以如果你复制/image.jpg和/a/image.jpg你会写原文.

  • 使用$ check = file_put_contents($ dest,file_get_contents($ src)); 并查看$ check的值.http://php.net/manual/en/function.file-put-contents.php说:该函数返回写入文件的字节数,如果失败则返回FALSE. (2认同)