php imagejpeg mime type - php

I am using php's imagejpeg to save a GD image resource to a file, doing this:
imagejpeg($im, '../images/' . $image_id . '.jpg');
It works fine, but according to my browser, it tries to read the file as text/plain:
Resource interpreted as image but transferred with MIME type text/plain.
Is there a step before saving the file that I am supposed to do to make sure it's using the right mine-type?
I am using windows (XAMPP), could it be a Windows issue?
EDIT: nope. I just tested in a linux server.
As far as the actual displaying, it's just plain html .
My upload code is supposed to saves= the file as a plain jpeg in the server. It's just not saving it with the right mime type.
Thanks

AFAIK, the Apache server - in standard out of the box configuration - should sent content-type headers purely based upon file extension. Apache shouldn't even even be looking at the contents or how it was originally generated/stored.
On my out-of-the-box Apache2, the file conf/mime.types contains the line:
image/jpeg jpeg jpg jpe
which ought to do it, right?
Can you post a test-case, say, a simple html page with two img tags: one for your generated image, and one for a standard image that seems to work fine?
One last thought: Does it occur in all browsers? Maybe it's a browser issue, not a server one?

It sounds like you're dumping the contents of the file to the browser and not actually telling the browser what type of file it is. Try adding a Content-type header before you output your image to the browser:
header('Content-type: image/jpeg');

Are you sure you aren't using the wrong file-extension name?
Otherwise, just put an normal image in the server and make sure the mime-types are properly configured.
It could also be that your image data from the manipulation is corrupted.

Related

Wrong Mime-type in uploaded file

I have form for file uploading in my website that i check mime-type of uploaded file to allow or refuse uploading it. It worked well until I've upgraded my firefox to 3.6.11 (in ubuntu). But now uploading Doc files is not possible. I checked the mime-type of file but it was 'application/x-php' instead of 'application/vnd.ms-word' or other msword mime-types.
I use
echo $_FILES[$fileName]['type'];
to see the mime-type. I upload the same file from firefox 8 and firefox 3.6 and the output of above code was:
FireFox 8: application/vnd.ms-word
FireFox 3.6: application/x-php
Is it a bug of firefox or I have to change my codes?
I use PHP.
$_FILES[...]['type'] is just arbitrary, user supplied, best-guess, unreliable (as you see) information supplied by the client which may or may not have anything to do with the actual file. Never use it.
Try to detect the MIME type yourself on the server. For example techniques, see How to get the content-type of a file in PHP?.

PHP output internal server file?

On my server I have a directory with music files, generally in .mp3 format. I'm writing a web application to search for and play these tracks.
All the files are stored, with their absolute server path, artist, album and title info in a MySQL database.
What I want to do is have a PHP file that "outputs" an mp3 file on the server that would normally be inaccessible from the web. So, this is what I want to achieve:
client requests play.php?id=10
PHP gets absolute server path from MySQL database where ID = 10
PHP outputs the file (which would really be at e.g. '/home/user/files/no_web/mp3/Thing.mp3')
To the client, it looks like there is an mp3 file called http://myserver.com/play.php?id=10 and it starts to play.
I'm sure this sort of thing is possible, just not sure how. Thanks in advance :)
You need to send correct content-type header and then just output the file:
header('Content-type: audio/mpeg3');
readfile('filename.mp3');
For reading the file and sending it, you can use the readfile function.
For setting the mime-type, so the browser actually knows what type of file is sent by the webserver, use the header function like:
header('Content-Type: audio/mpeg');
Additionally, you may also want to set the Content-Length HTTP header.
header('Content-Length: ' . filesize($filepath) );
If all you're trying to do is let the user download the mp3, just use the readfile command which will read the mp3 file and pass it along to the client. However you need to make sure to set the mime-type correctly.

Getting Safari to Write Out Correct FileName

I'm building a web app with PHP that uses Excel Writer (XML) for PHP to create an Excel file that the user can download. I've taken a look at the source code for the library, and all it does is echo the generated XML to standard output. Although the file being generated is an XML file, I specifically give it a name with a ".xls" extension so that after it's been downloaded the end-user can double-click it and it will launch Excel on their system and open the file successfully.
This works correctly when downloading the file with Firefox, Chrome and IE, but not with Safari. For example, if I set the name of the file to be 'File.xls', this is the name of the file that gets downloaded. But with Safari the name of the file ends up being 'File.xls.xml'.
The server is running Apache on Mac OS X. I thought that might matter as I'm guessing that the problem has to do with MIME types on the server, but I'm not at all sure. Perhaps I can do something with the link that appears on the page, or perhaps I need to edit a configuration file for Apache? Any help is appreciated.
You might be right, it could just be the MIME type that needs setting.
Try putting
header("Content-Type: application/vnd.ms-excel");
before the output code, that should force the browser to see the data as an Excel file.
If the code is setting Content-Type already and it's conflicting, then search the script for "Content-Type" and try changing it.
Also, if it's an XLSX file, then there's a different MIME type - application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

pdf errors with php, apache2

I'm seeing some strange behavior from some dynamically generated PDFs using the TCPDF library in PHP.
Standard lamp stack-- however, if you try to open the PDF from Windows with Acrobat Reader, it gives an error that the "file is damaged and could not be repaired". From Mac, Linux, etc. the file works fine, and opens fine. It also opens fine in Google Docs-- so clearly the PDF itself is ok.
Is it possible that the mime type (application/pdf) is causing problems in Windows?
Thanks
What browser on Windows? All? Or just one? My initial gut instict, is that the Windows browser is ignoring the encoding IF the content is being gzipped (Ignoring the Content-Encoding header). That's if you're even sending that header.
Open the file you downloaded in some text editor (Notepad, etc). The first few characters of the file should be %PDF-1. with another number after it. If it's not at the beginning, check to see if the file is gzipped (rename the file to blah.gz, and then run it through gzip to try to decode it). If that worked, then your problem is the browser ignoring the encoding.
IF it is ignoring the encoding, you need to not gzip the output of that PHP file. How you do that will depend on your server configuration.
Oh, and the application/pdf is the proper mime type. And the mime type is not your problem, since Acrobat is at least trying to open the file...

PHP - Dynamic images and IE 7

I have a webapplication where I read images from a sql 2008 database through the 'new' filestream functionality.
The thing here is, when you right-click and choose save image, IE gives error 800700de while with Firefox it works fine.
The img src is set to a php document which takes a few params to know where to look in the database and then prints the binary data. Please note that displaying the picture works fine in both browsers, this is not the issue. The issue is what I mentioned above.
Any idea why this could be? Is there any solution or a workaround for this?
Are you building the headers? If so that may be it. What headers are you sending?
Possible appropriate headers via: http://www.electrictoolbox.com/image-headers-php/
GIF:
header('Content-Type: image/gif');
JPEG:
header('Content-Type: image/jpeg');
PNG:
header('Content-Type: image/png');
It sounds like IE is going ape because it doesn't quite trust that your file is an image.
If you continue to have problems even after setting the Content-Type headers as others have explained, you can further fool the browser into thinking your script is an image by using a .png or .jpg extension, and then forcing that file to be executed as PHP.
For example, in .htaccess put this to disguise a PHP script as "image.png"
<Files image.png>
ForceType application/x-httpd-php
</Files>

Categories