Save a image from web given the image url

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?B?RulybmFz?=

    Save a image from web given the image url

    Hello all,

    How can I save a image that is on internet (eg:


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

    many thanks

  • Rik

    #2
    Re: Save a image from web given the image url

    On Tue, 24 Jul 2007 21:00:54 +0200, Férnas <garbage@widewe b.com.brwrote:
    Hello all,
    >
    How can I save a image that is on internet (eg:

    >
    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($urlin fo['path']);
    $target = $savedir.'/'.$filename;
    if(file_exists( $target) && !$overwrite){
    //break up file in parts:
    $pathinfo = pathinfo($targe t);
    //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($savedi r);
    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

    Comment

    • gosha bine

      #3
      Re: Save a image from web given the image url

      On 24.07.2007 21:00 Férnas wrote:
      Hello all,
      >
      How can I save a image that is on internet (eg:

      >
      I would like to save the image on my server...
      >
      many thanks
      >
      file_put_conten ts('mylogo.gif' ,
      file_get_conten ts('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

      Comment

      • =?iso-8859-1?B?RulybmFz?=

        #4
        Re: Save a image from web given the image url

        On 25 jul, 05:43, gosha bine <stereof...@gma il.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_conten ts('mylogo.gif' ,
        file_get_conten ts('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

        Comment

        • Krustov

          #5
          Re: Save a image from web given the image url

          <comp.lang.ph p>
          <gosha bine>
          <Wed, 25 Jul 2007 10:43:24 +0200>
          <46a70d29$0$112 20$6e1ede2f@rea d.cnntp.org>
          file_put_conten ts('mylogo.gif' ,
          file_get_conten ts('http://www.agalera.com .br/_img/layout/logo.gif'));
          >
          Good code .

          Comment

          Working...