从URL保存图像然后将其保存到目录php中

sm2*_*guy 5 php

可能重复:
使用php从php url保存图像

我如何使用PHP从另一个域(不在我的域/服务器上)保存/获取图像并将其保存在我的站点的目录中.

例如,图像的URL将是:

http://anothersite/images/goods.jpg
Run Code Online (Sandbox Code Playgroud)

我如何使用PHP来获取"good.jpg",并将其保存在我的目录中 www.mysite.com/directory/

我希望有人可以指导我.谢谢!

Cyc*_*ode 21

你应该可以使用file_get_contents这个.为了使用URL,请file_get_contents确保allow_url_fopen在您的php.ini文件中启用.

define('DIRECTORY', '/home/user/uploads');

$content = file_get_contents('http://anothersite/images/goods.jpg');
file_put_contents(DIRECTORY . '/image.jpg', $content);
Run Code Online (Sandbox Code Playgroud)

确保您对要存储图像的目录具有写入权限; 要使文件夹可写,您可以这样做:

chmod +w /home/users/uploads
Run Code Online (Sandbox Code Playgroud)

参考

  1. 的file_get_contents
  2. 于allow_url_fopen
  3. chmod命令