Jump to content

Checking MIME file type


sh0wtym3

Recommended Posts

Hey all,

 

I want to restrict uploads to mp3 and wav only using the script below:

 

//Begin

function upload(){   
   if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded
   
   
   
      $GLOBALS['msg'] = ""; //initiate the global message
      for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array
         $filen = $_FILES["item_file"]['name']["$j"]; //file name
         
              //Set allowed MIME types and get the filetype
         $allowed_mime_types = array('audio/mpeg','audio/x-wav');
         $filetype = filetype($_FILES["item_file"]['type']["$j"]);
         
         //Renames the music file
   $file = str_replace(' ', '_', $filen);
   $file = strtolower($file);
            //Remove everything except a-z, 0-9, and underscores
   $file = preg_replace('/[^\w\.]/i', '', $file);
         
         //Check type and BEGIN UPLOAD PROCESS
         if(in_array($filetype,$allowed_mime_types)) {

 

And I get the following error:

 

Warning: filetype() [function.filetype]: Lstat failed for audio/wav in /home1/mysite/public_html/test.php on line 109

 

Line 109 is

$filetype = filetype($_FILES["item_file"]['type']["$j"]);

 

It seems like its reading the filetype right (audio/wav) but I don't understand why it's kicking out this error ..?

Link to comment
https://forums.phpfreaks.com/topic/140044-checking-mime-file-type/
Share on other sites

filetype() gets the file type. The parameter going into it is "audio/wav" where it should be something like "path/sound.wav".

It also returns whether the path is a file or directory (or some other things). It does not return the mime type.

see http://us3.php.net/filetype

$filetype = filetype($_FILES["item_file"]['type']["$j"]);

to-

$filetype = $_FILES["item_file"]['type']["$j"];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.