Home › Forums › Archives › Computer Support › Computer Support Discussion › PHP and file content-type
- This topic has 4 replies, 3 voices, and was last updated 17 years ago by blindrain711.
-
AuthorPosts
-
March 1, 2007 at 3:37 pm #26502TigerbladeParticipant
I’m developing a CMS for a local musician and I’ve hit a snag. I’ve got an form where he can upload media for visitors to download — the PHP determines the file’s content type and stores the relevant details to an XML file.
That same XML file is used to generate a public list of available files. The files are stored in a non-browser-accessible directory, and referenced by a unique ID (the md5 hash of the filename). my problem is that once the file is saved and opened, I get an error that the file doesn’t match the content type.
here’s the relevant bits of code. first the basic part of the upload:Code:$mime = $_FILES[‘m_upload’][‘type’];
$name = $_FILES[‘m_upload’][‘name’];
$path = “/path/to/”. basename($name);if(!move_uploaded_file($_FILES[‘m_upload’][‘tmp_name’],$path)) {
//generate error messages, etc
} else {
//generate success messages, update XML
}that successfully uploads the file and writes the proper content to the XML file. in the case of an MP3, the MIME type is found to be ‘audio/mpeg’
here’s the download call ($item is an XML resource):Code:$file = $item->file; settype($file,’string’);
$mime = $item->mime; settype($mime,’string’);
$type = substr($mime,0,strpos($mime,’/’));
$path = “/path/to/$file”;
header(“Content-type: $mime”);
header(“Content-Length: ” . filesize($path) );
header(“Content-Transfer-Encoding: Binary”);
header(‘Content-Disposition: attachment; filename=”‘.$file.'”‘ );
@readfile($path);what am I missing? why is the file coming up as an incorrect filetype?
March 1, 2007 at 3:44 pm #158819DavidParticipantThe second and third lines of that code don’t make sense, neither are valid PHP.
Upload a text file and re-download it. is the content the same?
I also don’t see you setting the MIME type of the page anywhere. Take a look at PHP: header – Manual there is a mime-type example on the page.
March 1, 2007 at 3:53 pm #158821TigerbladeParticipantsorry, i had some issues with the browser while posting and didn’t get the full thing up. the full explanation and code snippets are now there.
what isn’t valid PHP? the part with $_FILES ?
edit: hmm, i’m wondering if this might have something to do with it. from php.net’s entry on $_FILES:
Quote:The mime type of the file, if the browser provided this information. An example would be “image/gif”. This mime type is however not checked on the PHP side and therefore don’t take its value for granted.I was hoping to sidestep having to actually set the mime type (not that it’s difficult, i just didn’t want to do it), but i’ll give that a try and go from there.
March 2, 2007 at 2:23 am #158820DavidParticipantinstead of printing to the header, print to the page itself and make sure the content type is what you expect it to me, and that there are no leading or trailing spaces or newlines. Also you can try manually setting the mime to what your file is, and see if it works. Alternatively, you should be able to give it a type of application/octet-stream. This will however, force most browsers to download the file and never play it or offer the option of playing it while it downloads.
March 28, 2007 at 3:11 pm #158822blindrain711Memberi use wfb from sourcefourge take a look at it you might find the neccesary code to do what you want
-
AuthorPosts
- You must be logged in to reply to this topic.