FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Interesting mistake - change extension on image





sonam
By mistake I am change uploaded image extension. I was sure I need some php function or whatever but image is coming up in browser with new extension. Shocked

For example uploaded file is company_name.gif
Code:
 $brand = company_name . '.jpg';
 move_uploaded_file($_FILES["upload_file"]["tmp_name"], some_dir . "/" . $brand);


Anyone have same experience?

Sonam
clubdeinovaristi
sonam wrote:
By mistake I am change uploaded image extension. I was sure I need some php function or whatever but image is coming up in browser with new extension. Shocked

For example uploaded file is company_name.gif
Code:
 $brand = company_name . '.jpg';
 move_uploaded_file($_FILES["upload_file"]["tmp_name"], some_dir . "/" . $brand);


Anyone have same experience?

Sonam


So you need to get the filename extension, don't you?
If this is the case, this could be a solution:
Code:
function extension($filepath)
{
    preg_match('/[^?]*/', $filepath, $matches);
    $string = $matches[0];

    $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);

    if(count($pattern) > 1)
    {
        $filenamepart = $pattern[count($pattern)-1][0];
        preg_match('/[^?]*/', $filenamepart, $matches);
        return strtolower($matches[0]);
    }
}


I took it from StackOverflow.

If this is not the case, please "explain better the matter".
jmraker
If the browser is displaying a gif file with a jpg extension it's because the browser's image handling core is smart enough to determine the file's graphic format based on the contents in the file's header. The different graphic formats have certain signatures in the first few bytes of the file.

Just because it works in some browsers doesn't mean all browsers will ignore the file extension.
sonam
@clubdeinovaristi
Thanks for reply but I don't need extension code. I just want to point out how some browsers display correct image with wrong extension because I was read there is few ways to change jpg in gif or png but with GD library, etc.

@jmraker
Quote:
Just because it works in some browsers doesn't mean all browsers will ignore the file extension.


This is good point. Thanks. Very Happy You are right it is better to keep original extension.

Sonam
Fire Boar
When sending anything to a web browser, a bunch of headers are sent. For example, for this image:

Very Happy

I see the following headers:

Code:
Date: Sun, 15 Apr 2012 23:31:07 GMT
Server: Apache/2
Last-Modified: Sun, 12 Mar 2006 18:18:16 GMT
Etag: "1110a02-ac-40ed03ff74a00"
Accept-Ranges: bytes
Content-Length: 172
Content-Type: image/gif

200 OK


Notice the Content-Type header, which tells my browser exactly what type the image file is. The headers are sent by the server, so it is up to the server to figure out what the file type is. As it happens, while Windows uses the extension to figure out the file type, Unix-based operating systems don't care, they look at the file itself, and from the first few bytes can figure it out. Most web servers run a Linux operating system, which is derived from Unix. Hence, they tend to get the Content-Type correct even when the extension is wrong.
sonam
Fire Boar wrote:

Notice the Content-Type header, which tells my browser exactly what type the image file is. The headers are sent by the server, so it is up to the server to figure out what the file type is. As it happens, while Windows uses the extension to figure out the file type, Unix-based operating systems don't care, they look at the file itself, and from the first few bytes can figure it out. Most web servers run a Linux operating system, which is derived from Unix. Hence, they tend to get the Content-Type correct even when the extension is wrong.


I am agree with you about headers but not about Windows and Linux because this mistake is coming out on my home server on Windows XP. I was try to open image with wrong extension in all major browsers and all displays image correct. I think this is not in correlation with operating system, it is in correlation with browsers. Even more Windows Image and Fax Viewer are display image correct but Fireworks and Photoshop cannot open it (but both are very old version). Laughing

Sonam
Fire Boar
sonam wrote:
I am agree with you about headers but not about Windows and Linux because this mistake is coming out on my home server on Windows XP. I was try to open image with wrong extension in all major browsers and all displays image correct. I think this is not in correlation with operating system, it is in correlation with browsers. Even more Windows Image and Fax Viewer are display image correct but Fireworks and Photoshop cannot open it (but both are very old version). Laughing

Sonam


It could be any number of things. The browser has an image viewer, which is given the file name and contents, including metadata. It's up to the application in question to decide how to show it. And it's up to the server to decide how to determine the file type for use in headers. The server might decide for itself, or it might appeal to external functions, say, provided by the operating system.
Related topics
Windows Tips&tricks!
CHMOD ?? Help me.. :(
PHpBB2
Tutorial: Image Rollovers w/ Javascript
Rank images contest
All About Metallica
Javascript rapid picture change
How to change extension php to html but the php script still
PHP Image Questions
how do i change between images..
Image Rollovers In Javascript With a Twist
uploading kompozer
IMAGE RESOLUTION CONCEPT IN PHOTOSHOP
Having browser not pull files from the cache.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.