PHP-直接从URL将图像复制到我的服务器
可能重复:PHP-直接从URL将图像复制到我的服务器,php,Php,可能重复: 我想有下面的PHP代码 例如,假设我有一个图像URL 如果我运行一个脚本,此映像将被复制并放在我的服务器上,位于具有777权限的文件夹中 可能吗?如果是的话,你能给我同样的方向吗 谢谢 伊恩 您必须将allow\u url\u fopen设置为on,这将解决您的问题。简言之,解决方案: $url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png'; $img = '/my/folder/my_image.gi
我想有下面的PHP代码 例如,假设我有一个图像URL 如果我运行一个脚本,此映像将被复制并放在我的服务器上,位于具有777权限的文件夹中 可能吗?如果是的话,你能给我同样的方向吗 谢谢 伊恩 您必须将
allow\u url\u fopen
设置为on
,这将解决您的问题。简言之,解决方案:
$url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png';
$img = '/my/folder/my_image.gif';
file_put_contents($img, file_get_contents($url));
如果使用PHP5(或更高版本),有两种方法 如果没有,请使用 从
…我想您可能需要澄清,$url的第二次使用与第一次不同。无法建立连接,因为目标计算机主动拒绝了它。I’我收到了这个错误。谢谢。。这只工作正常。。谢谢MichaelThanks。这对我来说就像一个符咒!非常感谢
copy()
url的trickinstead是否可以用位图图像完成?没有一个对我有效。。我正试图在codeignitier中成功。。但是什么也没有发生(谢谢多蒂,你的代码帮了我很多。在这个例子中,源文件和目标文件的文件扩展名不同。文件会从png转换成jpg吗?
$url = 'http://www.google.co.in/intl/en_com/images/srpr/logo1w.png';
$img = '/my/folder/my_image.gif';
file_put_contents($img, file_get_contents($url));
copy('http://www.google.co.in/intl/en_com/images/srpr/logo1w.png', '/tmp/file.png');
//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);
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);
$url = "http://www.example/images/image.gif";
$save_name = "image.gif";
$save_directory = "/var/www/example/downloads/";
if(is_writable($save_directory)) {
file_put_contents($save_directory . $save_name, file_get_contents($url));
} else {
exit("Failed to write to directory "{$save_directory}");
}