www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to save picture from url if it has no extension?

Hybrid View

  1. #1
    Join Date
    May 2011
    Posts
    58

    Exclamation How to save picture from url if it has no extension?

    How to save picture from url if it has no extension by using php of course.
    I use curl for save images (I need a extension of that files), but there is some sort of image that I can't save, the problem in it's url's: like that: http://url.com/image.jpg?blablabla and etc. What I must do with that images if I want to download it?
    Thank you.

  2. #2
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    Code:
    http://url.com/image.jpg?blablabla
    The server SHOULD respond to your request with a mime-type/content-type header.

    PHP Code:
    <?php

    $ch 
    curl_init("http://url.com/image.jpg?blablabla");
    curl_exec($ch);
    $info curl_getinfo($ch);
    echo 
    $info["CURLINFO_CONTENT_TYPE"];
    curl_close($ch); // always close handle 

    ?>
    Extensions are not really important, from what I understand they only serve two purposes:
    1) to help end-users identify types of files without having to open them
    2) for operating systems to add some type of image/logo and default program associated with an extension.

    They carry absolutely zero weight as far as the file's actual contents go. The only reason your .php files are executed using PHP is because there is an apache directive more or less like this:

    Code:
    <FilesMatch "\.php$">
      AddHandler x-httpd-php5.3 .php
    </FilesMatch>
    You can rename all of your php files jpg and add this directive:

    Code:
    <FilesMatch "\.jpg$">
      AddHandler x-httpd-php5.3 .jpg
    </FilesMatch>


    Cheers
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,924
    You could also try loading it into getimagesize(), and see what it finds. It returns an array, with element "2" being a PHP constant for the image type, and element 'mime' being the MIME type (or the function returns Boolean false if it does not recognize it as a known image type).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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