Can someone give an example download, save and rename image from url to server code?

Discussion in 'PHP' started by aandamar, Sep 16, 2011.

  1. #1
    I've tried using curl to download and save image from url, but this totally failed.
    The page shown picture like an image opened using notepad :(
    this is the code I've tried:
    $linkgambar = $result['gambar'];
    $name = get_the_title();
    $ch = curl_init($linkgambar);
    $fp = fopen('/pict/', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    Code (markup):
    how I can save the image using $name without know the file extension?
    can someone give an example of function to download, rename and save the image to server with $name as the new image name?

    thanks
     
    aandamar, Sep 16, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Is $result['gambar'] the location of the image file, or the page on which the image appears? If it's the page, you're saving text, not an image. You have to give it the file name of the file you want to download. (The page the image is on just has an <img /> tag, not an image.)
    
    $url  = 'http://www.gambar.com/images/myfile.jpg';
    $path = '/path/to/local/copy/of/myfile.jpg';
    $fp = fopen($path, 'wb');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    $data = curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    
    PHP:
     
    Rukbat, Sep 16, 2011 IP
  3. aandamar

    aandamar Well-Known Member

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    3
    #3
    thanks rukbat!
    yes, $result['gambar'] is the location of image file.
    How I can save the image name with format automatically, the file name using $name?

    thanks
     
    aandamar, Sep 16, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    With the code I gave you. If you don't know the extension of the file you can read the beginning of it and figure out the file type, then rename it.
     
    Rukbat, Sep 16, 2011 IP
  5. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    Something like this will upload an image and overwrite the existing file with the new ones. addin file-type extensions on your own, set up the submit HTML for the upload. simple

    /// leave the name newimage.jpg
    $newimage = "Your_Image_Folder/newimage.jpg";
    $result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
    if(empty($result))
    $error["result"] = "There was an error moving the uploaded file.";
     
    ezprint2008, Sep 17, 2011 IP
  6. aandamar

    aandamar Well-Known Member

    Messages:
    243
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    3
    #6
    aandamar, Sep 22, 2011 IP
  7. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #7
    Please use this curl_get method I wrote for you, it sends useragent in header, better is put following in a file to include, example of usage is at bottom.
    A word-press plugin can be created easily. So far this is code for image fetching and saving on server. If you need WP plugin, let me know.

        function curl_get($page)
        {
            $ch = curl_init();
    
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
            curl_setopt($ch, CURLOPT_URL,$page);
            curl_setopt($ch, CURLOPT_POST, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            #curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
            curl_setopt($ch, CURLOPT_HEADER ,0);        
    
    
            $data = curl_exec ($ch);
            curl_close ($ch); 
    
            return $data;           
        }        
        function get_image_and_save($image_url, $save_to)
        {
            $data = curl_get($image_url);
            if (!$data ||
            return (!$data || $data=='' ) ? FALSE : file_put_contents($save_to, $data);
        }
    
    PHP:
        #using it
        $res = get_image_and_save("http://www.babyannouncementwording.org/wp-content/uploads/baby2.jpg", "./upload_dir/myfile_.jpg");
        if (!$res)
            echo "Some error occurred!";
    PHP:
    I hope it helps.
     
    Vooler, Sep 23, 2011 IP
  8. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #8
    Try this
    file_put_contents("baby2.jpg", file_get_contents("http://www.babyannouncementwording.org/wp-content/uploads/baby2.jpg"));
    
    
    
    Code (markup):
     
    gvre, Sep 24, 2011 IP