从 URL 保存图像,然后将其保存到目录 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8382098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
save an image from a URL then save it into a directory php
提问by sm21guy
Possible Duplicate:
save image from php url using php
可能的重复:
使用 php 从 php url 保存图像
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.
我如何使用 php 从另一个域(不在我的域/服务器上)保存/抓取图像并将其保存在我网站的目录中。
The URL of the image for example , will be :
例如,图像的 URL 将是:
http://anothersite/images/goods.jpg
How can i use PHP to grab "good.jpg" ,and save it in my directory which is www.mysite.com/directory/
我如何使用 PHP 来抓取“good.jpg”,并将其保存在我的目录中 www.mysite.com/directory/
I hope someone could guide me. Thanks!
我希望有人可以指导我。谢谢!
回答by Cyclonecode
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.
你应该可以使用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);
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
References
参考
回答by Sheikh Abdur Rahman
This link may be answer your question:
此链接可能会回答您的问题:
http://stackoverflow.com/questions/909374/copy-image-from-remote-server-over-http
to me the following code should serve ur need:
对我来说,以下代码应该可以满足您的需求:
$url = "http://other-site/image.png";
$dir = "/my/local/dir/";
$lfile = fopen($dir . basename($url), "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_FILE, $lfile);
fclose($lfile);
curl_close($ch);