Results 1 to 2 of 2

Thread: Save image without extension from url in directory

  1. #1
    Join Date
    Apr 2014

    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.



    $fullpath = "images_saved";

    foreach($my_img as $i){

    echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded Successfully</h3>';
    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);
    curl_close ($ch);
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);

  2. #2
    Join Date
    Mar 2005
    Behind you...
    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) {
    $fullpath != "" && $fullpath$fullpath $fullpath "/" basename($my_img);    
    file_exists($fullpath)) unlink($fullpath);
    $newImage file_get_contents($my_img);

    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:

    $fullpath "images_saved";

    $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>';

    image_save_from_url($my_img$fp) {
    $fp != "" && $fp$fp $fp "/" basename($my_img);    
    file_exists($fp)) unlink($fp);
    $newImage file_get_contents($my_img);
    "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