简体   繁体   English

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

[英]Copying an image from one server to another using PHP

I'm trying to copy an image from a remote server with the following code: 我正在尝试使用以下代码从远程服务器复制图像:

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

Unfortunately I keep getting the following error: 不幸的是我一直收到以下错误:

Warning: file_put_contents(/server/location/upload/) [function.file-put-contents]: failed to open stream: Is a directory in /server/location/myscript.php on line 220 警告:file_put_contents(/ server / location / upload /)[function.file-put-contents]:无法打开流:第220行/server/location/myscript.php中的目录

Do you have any ideas how to get around this? 你有什么想法如何解决这个问题?

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

You need to specify the filename. 您需要指定文件名。 I added basename($src) which will write to the same filename that the original was. 我添加了basename($src) ,它将写入与原始文件名相同的文件名。 Be careful if you're copying from other directories, basename() only returns the filename so if you copy /image.jpg and /a/image.jpg you'll write over the original. 如果你从其他目录复制,要小心, basename()只返回文件名,所以如果你复制/image.jpg和/a/image.jpg你会写原文。

This is because $dest is a directory, not a file. 这是因为$ dest是一个目录,而不是一个文件。 You could either manually specify the filename or use basename() 您可以手动指定文件名或使用basename()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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