www.webdeveloper.com
Results 1 to 2 of 2

Thread: Save image without extension from url in directory

  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Save image without extension from url in directory

    How could I save an image, without extension, from an URL and save it in a directory?

    I am using php code below it works, but not when the url ends at .jpg?test.


    <?php
    $my_img[]='http://images.askmen.com/galleries/cobie-smulders/picture-1-134686094208.jpg';

    $my_img[]='http://images.askmen.com/galleries/demi-lovato/picture-1-134212249378.jpg';

    $fullpath = "images_saved";

    foreach($my_img as $i){
    image_save_from_url($i,$fullpath);

    if(getimagesize($fullpath."/".basename($i))){
    echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded Successfully</h3>';
    }else{
    echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
    }
    }

    function image_save_from_url($my_img,$fullpath){
    if($fullpath!="" && $fullpath){
    $fullpath = $fullpath."/".basename($my_img);
    }
    $ch = curl_init ($my_img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
    unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
    }
    ?>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    901
    I don't really have time to upload and test this code, but I see no reason why this won't work for you:
    PHP Code:
    function image_save_from_url($my_img$fullpath) {
        if(
    $fullpath != "" && $fullpath$fullpath $fullpath "/" basename($my_img);    
        if(
    file_exists($fullpath)) unlink($fullpath);
        
        
    $newImage file_get_contents($my_img);
        return 
    file_put_contents($fullpath$newImage);

    The image_save_from_url() function probably doesn't need the return value (thus 'return' could be removed from the function on the last line). However, I added it because it would allow you to optimize another area of your code since the function should return FALSE when the file is not successfully written (and a number when it is successfully written).
    PHP Code:
    <?php
        $my_img
    []='http://images.askmen.com/galleries/cobie-smulders/picture-1-134686094208.jpg';
        
    $my_img[]='http://images.askmen.com/galleries/demi-lovato/picture-1-134212249378.jpg';

        
    $fullpath "images_saved";

        foreach(
    $my_img as $i) {
            echo (
    image_save_from_url($i$fullpath)) ? '<h3 style="color: green;">Image ' basename($i) . ' Downloaded Successfully</h3>' '<h3 style="color: red;">Image ' basename($i) . ' Download Failed</h3>';
        }

        function 
    image_save_from_url($my_img$fp) {
            if(
    $fp != "" && $fp$fp $fp "/" basename($my_img);    
            if(
    file_exists($fp)) unlink($fp);
            
            
    $newImage file_get_contents($my_img);
            return 
    file_put_contents($fp$newImage);
        }
    ?>
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles