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

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

问题描述:

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

Possible Duplicate:
save image from php url using php

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

How can i use php to save/grab an image from another domain(not on my domain/server) and save it in a directory of my site.

例如图像的URL为:

http://anothersite/images/goods.jpg

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

How can i use PHP to grab "good.jpg" ,and save it in my directory which is www.mysite.com/directory/

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

I hope someone could guide me. Thanks!

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

You should be able to use file_get_contents for this one. In order to use an URL with file_get_contents make sure allow_url_fopen is enabled in you php.ini file.

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

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

确保对要存储图像的目录具有写权限;要使该文件夹可写,您可以执行以下操作:

Make sure that you have write permission to the directory where you want to store the image; to make the folder writable you could do this:

chmod +w /home/users/uploads

参考

  1. file_get_contents
  2. allow_url_fopen
  3. chmod命令
  1. file_get_contents
  2. allow_url_fopen
  3. chmod command