我正试图用以下代码从远程服务器复制图像:
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/";
file_put_contents($dest, file_get_contents($src));
不幸的是,我一直得到以下错误:
警告:file_put_contents(/server/location/upload/)[function.file put contents]:无法打开流:是第220行/server/location/myscript.php中的目录
你有什么办法绕过这个吗?
$src = "http://www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename($src);
file_put_contents($dest, file_get_contents($src));
您需要指定文件名。我添加了basename($src)
,它将写入与原始文件相同的文件名。如果从其他目录复制,请小心,basename()
只返回文件名,因此如果复制/image.jpg和/a/image.jpg,则会重写原始文件。