Using PHP and CURL to get image from remote url

Discussion in 'PHP' started by GeelongTECH, Mar 18, 2011.

  1. #1
    Hello,

    I need a PHP code that can get the image url from "$_POST['big_img']"(eg: http://i.dpstatic.com/misc/dps_logo.png) and more it to my server, and rename the image name to a random (eg: rand().dps.logo.png). The directory is /home/sharing/sub-domains/tuts/public_html/images/big/
     
    GeelongTECH, Mar 18, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I don't see why CURL should be a must. Here is my solution to your problem :

    
    <?php
        $path = '/home/sharing/sub-domains/tuts/public_html/images/big/';
        if(isset($_POST['big_img']))
        {
            $image = file_get_contents(urldecode($_POST['big_img']));
            if($image !== false)
            {
                if(strpos($_POST['big_img'],"/") !== false)
                {
                    $name = explode("/",$_POST['big_img']);
                    $total = count($name);
                    $handle = fopen($path.rand()."_".$name[$total-1],"w") or die("Could not create : ".$path.rand()."_".$name[$total-1]);
                    if($handle !== false)
                    {
                        fwrite($handle,$image);
                        fclose($handle);
                        echo 'The file has been successfully saved !';
                    }
                }
            } else {
                echo 'Wrong path !';
            }
        }
    ?>
    
    PHP:
     
    tvoodoo, Mar 19, 2011 IP
  3. GeelongTECH

    GeelongTECH Peon

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    My Host, doessn't allow file_get_contents
     
    GeelongTECH, Mar 19, 2011 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    <?php
        $path = '/home/sharing/sub-domains/tuts/public_html/images/big/';
        if(isset($_POST['big_img']))
        {
            //$image = file_get_contents(urldecode($_POST['big_img']));
            $curl_handle=curl_init(urldecode($_POST['big_img']));
            curl_setopt($curl_handle, CURLOPT_NOBODY, true);
            $result = curl_exec($curl_handle);
            $retcode = false;
            if($result !== false)
            {
                $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
                if($status == 200)
                    $retcode = true;    
            }
            curl_close($curl_handle);
            
            if($retcode)
            {
                $curl_handle=curl_init();
                curl_setopt($curl_handle,CURLOPT_URL,urldecode($_POST['big_img']));
                curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
                curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
                $image = curl_exec($curl_handle);
                curl_close($curl_handle);
                
                if($image !== false)
                {
                    if(strpos($_POST['big_img'],"/") !== false)
                    {
                        $name = explode("/",$_POST['big_img']);
                        $total = count($name);
                        $handle = fopen($path.rand()."_".$name[$total-1],"w") or die("Could not create : ".$path.rand()."_".$name[$total-1]);
                        if($handle !== false)
                        {
                            fwrite($handle,$image);
                            fclose($handle);
                            echo 'The file has been successfully saved !';
                        }
                    }
                }
            } else {
                echo 'File not found !';
            }
        }
    ?>
    
    PHP:
     
    tvoodoo, Mar 19, 2011 IP
  5. GeelongTECH

    GeelongTECH Peon

    Messages:
    154
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, got it work :D
     
    GeelongTECH, Mar 19, 2011 IP
  6. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Your welcome !
     
    tvoodoo, Mar 19, 2011 IP
  7. phpBeginner

    phpBeginner Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,

    I want this same functionality on submit of text box:
    The image url will be input in this text box
    How do I achieve the same?

    <tr valign="top">
    <th scope="row"><label><?php _e( 'External Image', 'testify' ) ?></label></th>
    <td>
    <input type="text" name="testify_meta[ext_image]" id="ext_image" value="<?php if( $project && !empty($meta['ext_image']) ) echo $meta['ext_image']; ?>" />


    </td>
    </tr>

    <div class="testify-button"><input type="submit" name="save_project" value="<?php _e( 'Save Project', 'testify' ) ?>"></div>
    <input type="hidden" name="project_id" value="<?php if ($project) echo $project->ID ?>">
    </form>

    </div>
     
    phpBeginner, Apr 8, 2011 IP