首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP -直接从URL将图像复制到我的服务器

PHP -直接从URL将图像复制到我的服务器
EN

Stack Overflow用户
提问于 2011-06-10 21:21:57
回答 5查看 200.4K关注 0票数 70

可能重复:

save image from php url using php

我想有下面的PHP代码。

假设我有一个图像URL,例如http://www.google.co.in/intl/en_com/images/srpr/logo1w.png

如果我运行一个脚本,这个镜像将被复制并放在我的服务器上拥有777权限的文件夹中。

有可能吗?如果是的话,你能给我指明方向吗?

谢谢,

伊恩

EN

回答 5

Stack Overflow用户

发布于 2011-06-10 21:26:46

两种方式,如果你使用的是PHP5 (或更高版本)

代码语言:javascript
复制
copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.png');

如果不是,请使用file_get_contents

代码语言:javascript
复制
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.png", "w");
fwrite($fp, $content);
fclose($fp);

来自this SO post

票数 150
EN

Stack Overflow用户

发布于 2011-06-10 21:28:11

来自Copy images from url to server, delete all images after

代码语言:javascript
复制
function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('tmp/'.$imagename,$image);       
票数 32
EN

Stack Overflow用户

发布于 2011-06-10 21:25:53

代码语言:javascript
复制
$url="http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents=file_get_contents($url);
$save_path="/path/to/the/dir/and/image.jpg";
file_put_contents($save_path,$contents);

必须将allow_url_fopen设置为on

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6306935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档