By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,567 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,567 IT Pros & Developers. It's quick & easy.

Save a image from web given the image url

P: n/a
Hello all,

How can I save a image that is on internet (eg:
http://www.agalera.com.br/_img/layout/logo.gif)???

I would like to save the image on my server...

many thanks

Jul 24 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Rik
On Tue, 24 Jul 2007 21:00:54 +0200, Férnas <ga*****@wideweb.com.brwrote:
Hello all,

How can I save a image that is on internet (eg:
http://www.example.com/_img/layout/logo.gif)???

I would like to save the image on my server...
If allow_url_fopen[1] is enabled:
<?php

$url = 'http://example.com/image.jpg';
$savedir = '/tmp'; //or anything else you want
$overwrite = true; //or false if image has to be renamed on duplicate

$urlinfo = parse_url($url);
$filename = basename($urlinfo['path']);
$target = $savedir.'/'.$filename;
if(file_exists($target) && !$overwrite){
//break up file in parts:
$pathinfo = pathinfo($target);
//max 50 tries
$max = 50;
//loop
for($i = 1;$i<=$max;$i++){
$target = $pathinfo['dirname']. '/' . $pathinfo['filename'] . '[' . $i
.. '].' . $pathinfo['extention'];
//break on success, do not use file_exists to avoid race
$fh = @fopen($target,'x');
if($fh) break;
}
//alternatively, if you don't care about the name, you can just:
//$target = tempnam($savedir);
if(!$fh) die('Too many retries, no unique filename found.');
} else {
$fh = fopen($target,'w');
}
$check = fwrite($fh,file_get_contents($url));
fclose($fh);
echo (($check) ? 'Successfully saved '.$target : 'Failure');
?>

[1]http://nl2.php.net/manual/en/ref.filesystem.php
--
Rik Wasmus
Jul 24 '07 #2

P: n/a
On 24.07.2007 21:00 Férnas wrote:
Hello all,

How can I save a image that is on internet (eg:
http://www.agalera.com.br/_img/layout/logo.gif)???

I would like to save the image on my server...

many thanks
file_put_contents('mylogo.gif',
file_get_contents('http://www.agalera.com.br/_img/layout/logo.gif'));

;)
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Jul 25 '07 #3

P: n/a
On 25 jul, 05:43, gosha bine <stereof...@gmail.comwrote:
On 24.07.2007 21:00 Férnas wrote:
Hello all,
How can I save a image that is on internet (eg:
http://www.agalera.com.br/_img/layout/logo.gif)???
I would like to save the image on my server...
many thanks

file_put_contents('mylogo.gif',
file_get_contents('http://www.agalera.com.br/_img/layout/logo.gif'));

;)

--
gosha bine

makrell ~http://www.tagarga.com/blok/makrell
php done right ;)http://code.google.com/p/pihipi
Thank's all.. It worked!!!

your help was very appreciated...

Regards

Jul 25 '07 #4

P: n/a
<comp.lang.php>
<gosha bine>
<Wed, 25 Jul 2007 10:43:24 +0200>
<46***********************@read.cnntp.org>
file_put_contents('mylogo.gif',
file_get_contents('http://www.agalera.com.br/_img/layout/logo.gif'));
Good code .
Jul 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.