SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php save image to folder from web

    I'm looking to be able to get images from a remote url and save them to a folder on my site...

    I see lots of examples of file upload, but not where the file is from a remote url.

    Any clues would be very helpful!

  2. #2
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://php.net/fgets

    From the manual
    Code:
    <?php
    $handle = @fopen("/tmp/inputfile.txt", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            echo $buffer;
        }
        fclose($handle);
    }
    ?>
    If your PHP is able to open remote files, then you can replace /tmp/inputfile.txt with the remote URL.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  3. #3
    SitePoint Addict
    Join Date
    Sep 2007
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use CURL, with the CURLOPT_BINARYTRANSFER and CURLOPT_RETURNTRANSFER options. Additionally, if the file type of the image varies, you might also wanna use curl_getinfo function with CURLINFO_CONTENT_TYPE option to get the file type.

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dmsuperman View Post
    http://php.net/fgets

    From the manual
    Code:
    <?php
    $handle = @fopen("/tmp/inputfile.txt", "r");
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            echo $buffer;
        }
        fclose($handle);
    }
    ?>
    If your PHP is able to open remote files, then you can replace /tmp/inputfile.txt with the remote URL.
    thanks...

    so i have tried to save the image to the folder...i get a file saved, but it doesnt read back when viewed on the browser...taking it the image file hasnt been created correctly?

    PHP Code:
    $handle = @fopen($img"r");
    if (
    $handle) {
       while (!
    feof($handle)) {
           
    $buffer fgets($handle4096);
           
    //echo $buffer;
       
    }
       
    fclose($handle);
    }

       
    $filename "images/".$id.".jpg";
       
    $mystring fopen($filename"wb");
       
    $handle fopen($filename"wb");
       
    $numbytes fwrite($handle$buffer);
       
    fclose($handle); 

  5. #5
    SitePoint Evangelist dmsuperman's Avatar
    Join Date
    Feb 2005
    Location
    A box
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since you aren't echoing it, but storing it in the buffer, than it from $buffer = fgets to $buffer .= fgets. this way, it will append to $buffer each time.
    <(^.^<) \(^.^\) (^.^) (/^.^)/ (>^.^)>
    Core 2 Duo E8400 clocked @ 3.375GHz, 2x2GB 800MHz DDR2 RAM
    5x SATA drives totalling 2.5TB, 7900GS KO, 6600GT

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah thx that sorted it

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually if i now want to check to see if the image size is what i need...if not resize to specific width/height...how would i do this?

    I remember there is a php function to get filesize:

    PHP Code:
    list($width$height$type$attr) = getimagesize($img); 
    this will give me the width and height, so i can do conditional logic to see if i need to resize...

    so to resize...how can i make this work with my stored $buffer (found this)?

    PHP Code:
    $src imagecreatefromjpeg($buffer);
    list(
    $width,$height)=getimagesize($buffer);
    $newwidth=100;
    $newheight=($height/$width)*100;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $filename "images/"$_FILES['uploadfile']['name'];
    imagejpeg($tmp,$filename,100);
    imagedestroy($src);
    imagedestroy($tmp); 

  8. #8
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Damn! Just realised that this way to grab the image is fine when executing the script via the browser, but when done via cronjob, the remote server is blocking the connection (to stop hotlinking presumabley)

    Think i may have to use curl instead...

    I found this:

    PHP Code:
    <?php
    $image_url 
    "http://example.com/image.jpg";
    $ch curl_init();
    $timeout 0;
    curl_setopt ($chCURLOPT_URL$image_url);
    curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);

    // Getting binary data
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_BINARYTRANSFER1);

    $image curl_exec($ch);
    curl_close($ch);

    // output to browser
    header("Content-type: image/jpeg");
    print 
    $image;
    ?>
    How can i adapt this to save the file to my images folder?

  9. #9
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hei, here is the code to save it to a directory on your server, after fetching image:
    $f = fopen('/home/www/path/image.jpg', 'w');
    fwrite($f, $image);
    fclose($f);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •