I Need To Download Image From Web To Server Using Php

Discussion in 'PHP' started by deriklogov, Mar 4, 2007.

  1. #1
    Hi,

    Basicly what I need says in the title: I NEED TO DOWNLOAD IMAGE FROM WEB TO SERVER USING PHP, is that possible ? if yes, does anyone has simple example of the code which can do that ?
     
    deriklogov, Mar 4, 2007 IP
  2. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SEARCH PHP.NET FOR FOPEN AND FGET. You will find sample code there.
     
    Icheb, Mar 4, 2007 IP
  3. adsblog

    adsblog Active Member

    Messages:
    659
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #3
    adsblog, Mar 4, 2007 IP
  4. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #4
    I check it out those , but I cant find any samples over there that would download image and save it to disk, just some examples of getting .html documents
     
    deriklogov, Mar 4, 2007 IP
  5. Jim_

    Jim_ Peon

    Messages:
    72
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Something like this should work.
    <?php
    $filelocation = "http://www.site.com/folder/image.gif";
    $foldertosave = "./images/";
    
    $open = fopen($filelocation, "rb");
    $contents = stream_get_contents($open);
    fclose($open);
    $save = fopen(array_pop(split("/", $filelocation)), "a");
    fwrite($save,$contents);
    fclose($save);
    ?>
    PHP:
    You'll probably want to do some verification to make sure that the file being uploaded is in fact an image. The best way I can think of doing that is to open the image using the GD library and if it returns an error, the file is not an image.
     
    Jim_, Mar 4, 2007 IP
  6. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #6
    Thank Jim, Its working
     
    deriklogov, Mar 4, 2007 IP
  7. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So? It's all a file, nothing else.
     
    Icheb, Mar 4, 2007 IP
  8. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I would recommend cURL using the CURLOPT_INFILE option.
     
    Chemo, Mar 5, 2007 IP
  9. ccharp

    ccharp Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    nice one guys, i've been looking for something like this as well...thanks
     
    ccharp, Mar 6, 2007 IP
  10. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    <?php

    $filename=$_REQUEST['filename'];
    $filename = realpath($filename);

    $file_extension = strtolower(substr(strrchr($filename,"."),1));

    /****Set Header Content Type based on File Extension****/
    switch ($file_extension) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpe":
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
    }
    if (!file_exists($filename)) {
    die("NO FILE HERE");
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",false);
    header("Content-Type: $ctype");
    header('Content-Description: File Transfer');
    header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filename));
    set_time_limit(0);
    @readfile("$filename") or die("File not found.");

    ?>
     
    jitesh, Mar 14, 2007 IP
  11. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,079
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #11
    Thank You for Your Post, I already found one and its working fine.
     
    deriklogov, Mar 14, 2007 IP