使用PHP以编程方式保存远程图像

使用PHP以编程方式保存远程图像,php,image,upload,readfile,output-buffering,Php,Image,Upload,Readfile,Output Buffering,我正在尝试将一些内容从一个资源迁移到另一个资源,并且需要保存位于远程资源的一些图像(数百个) 假设我只有图像的URL: https://www.example.com/some_image.jpg 我想使用PHP将其保存到文件系统中 如果我上传图像,我基本上会执行以下操作: <input type="file" name="my_image" /> move_uploaded_file($_FILES['my_image']['tmp_name'], '/my_img_direc

我正在尝试将一些内容从一个资源迁移到另一个资源,并且需要保存位于远程资源的一些图像(数百个)

假设我只有图像的URL:

https://www.example.com/some_image.jpg
我想使用PHP将其保存到文件系统中

如果我上传图像,我基本上会执行以下操作:

<input type="file" name="my_image" />

move_uploaded_file($_FILES['my_image']['tmp_name'], '/my_img_directory');
这当然不起作用,因为
move\u uploaded\u file()
没有将输出缓冲区作为第一个参数


基本上,在这种方法下,我需要将
$img
放入
$\u文件[]
数组中。或者可以采用其他方法

您可以使用PHP的复制功能将远程文件复制到服务器上的某个位置:

$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image); //Where to save the image on your server
copy("https://example.com/some_image.jpg", "/path/to/file.jpg");

您可以使用PHP的复制功能将远程文件复制到服务器上的某个位置:

copy("https://example.com/some_image.jpg", "/path/to/file.jpg");