Current location: Hot Scripts Forums » Programming Languages » PHP » MIME Type arrays


MIME Type arrays

Reply
  #1 (permalink)  
Old 07-25-04, 04:24 PM
therat's Avatar
therat therat is offline
Newbie Coder
 
Join Date: Jan 2004
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
MIME Type arrays

I am trying to use an array of mime types to check when uploading files. The class I am using woks perfectly apart from checking against more than one mime type. The page calling the class is as follows
PHP Code:

$fu = new FileUpload(); 

    
$fu->max_filesize(5242880); 
    
$fu->max_image_size(15003000); // ($width, $height) 
    
$vtypes "image/pjpeg"
    
$fu->upload("imagefile"$vtypes".jpg"); 
    
$fu->save_file("uploads/"); 
    if (
$fu->error) { 
       echo 
$fu->error "<br>"
    } else { 
            
// Successful upload! 
            
print($fu->file['name'] . " was successfully uploaded!<br><br>\n"); 
            
// If it's an image, let's display the file 
            
if(stristr($fu->file['type'], "image")) { 
            echo 
"<img src=\"uploads/" $fu->file['name'] . "\" border=\"0\" alt=\"\"><br><br>\n"
            } 
    } 
The $vtypes line is where I am adding the valid mime types. If I change it to
PHP Code:

$vtypes = array('image/pjpeg' 'image/gif' ); 

or any other combination I get an error saying Only Array files may be uploaded. The actual upload class is this, I did not write all of this, most is from various other scripts from the net.
PHP Code:

<?php 

class FileUpload 

    var 
$path
    var 
$acceptable_file_types
    var 
$error
    var 
$max_filesize
    var 
$max_image_width
    var 
$max_image_height

    
//Class constructor method 
    
function FileUpload() { 
        
$this->error   ''
    } 

    
//Sets the maximum file size in bytes 
    
function max_filesize($size) { 
        
$this->max_filesize = (int) ($size); 
    } 

    
//Sets the maximum image size. Only used if upload is an image file 
    
function max_image_size($width$height) { 
        
$this->max_image_width = (int) ($width); 
        
$this->max_image_height = (int) ($height); 
    } 

    
//Checks if file is allowed and uploads to PHP's default directory 
    
function upload($filename=''$accept_type=''$extension='') { 
        
$this->acceptable_file_types trim($accept_type); // used by error messages 
      
if (!isset($_FILES) || !is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) { 
         
$this->error $this->get_error(0); 
         
$this->accepted  FALSE
         return 
FALSE
      } 
      
// Copy PHP's global $_FILES array to a local array 
      
$this->file $_FILES[$filename]; 
      
$this->file['file'] = $filename
      
// Initialize empty array elements 
      
if (!isset($this->file['extention'])) $this->file['extention'] = ""
      if (!isset(
$this->file['type']))      $this->file['type']      = ""
      if (!isset(
$this->file['size']))      $this->file['size']      = ""
      if (!isset(
$this->file['width']))     $this->file['width']     = ""
      if (!isset(
$this->file['height']))    $this->file['height']    = ""
      if (!isset(
$this->file['tmp_name']))  $this->file['tmp_name']  = ""
      if (!isset(
$this->file['raw_name']))  $this->file['raw_name']  = ""
      
// test max size 
      
if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) { 
         
$this->error $this->get_error(1); 
         
$this->accepted  FALSE
         return 
FALSE
      } 
      if(
stristr($this->file["type"], "image")) { 
          
         
/* IMAGES */ 
         
$image getimagesize($this->file["tmp_name"]); 
         
$this->file["width"]  = $image[0]; 
         
$this->file["height"] = $image[1]; 
          
         
// test max image size 
         
if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) { 
            
$this->error $this->get_error(2); 
            
$this->accepted  FALSE
            return 
FALSE
         } 
         
// Image Type is returned from getimagesize() function 
         
switch($image[2]) { 
            case 
1
               
$this->file["extention"] = ".gif"; break; 
            case 
2
               
$this->file["extention"] = ".jpg"; break; 
            case 
3
               
$this->file["extention"] = ".png"; break; 
            case 
4
               
$this->file["extention"] = ".swf"; break; 
            case 
5
               
$this->file["extention"] = ".psd"; break; 
            case 
6
               
$this->file["extention"] = ".bmp"; break; 
            case 
7
               
$this->file["extention"] = ".tif"; break; 
            case 
8
               
$this->file["extention"] = ".tif"; break; 
            default: 
               
$this->file["extention"] = $extention; break; 
         } 
      } elseif(!
ereg("(\.)([a-z0-9]{3,5})$"$this->file["name"]) && !$extention) { 
         
// Try and autmatically figure out the file type 
         // For more on mime-types: [url]http://httpd.apache.org/docs/mod/mod_mime_magic.html[/url] 
         
switch($this->file["type"]) { 
            case 
"text/plain"
               
$this->file["extention"] = ".txt"; break; 
            case 
"text/richtext"
               
$this->file["extention"] = ".txt"; break; 
            default: 
               break; 
         } 
      } else { 
         
$this->file["extention"] = $extention
      } 
      
// check to see if the file is of type specified 
      
if($this->acceptable_file_types) { 
         if(
trim($this->file["type"]) && (stristr($this->acceptable_file_types$this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) { 
            
$this->accepted TRUE
         } else { 
            
$this->accepted FALSE
            
$this->error $this->get_error(3); 
         } 
      } else { 
         
$this->accepted TRUE
      } 
       
      return (bool) 
$this->accepted
    } 

    
//Cleans the filename and copies file from temp location to $path 
    
function save_file($path) { 
        if (
$this->error) { 
            return 
false
        } 
      if (
strlen($path)>0) { 
         if (
$path[strlen($path)-1] != "/") { 
            
$path $path "/"
         } 
      } 
      
$this->path $path;    
      
$copy       "";    
      
$n          1;    
      
$success    false;    
             
      if(
$this->accepted) { 
         
// Clean up file name (only lowercase letters, numbers and underscores) 
         
$this->file["name"] = ereg_replace("[^a-z0-9._]"""str_replace(" ""_"str_replace("%20""_"strtolower($this->file["name"])))); 
          
         
// Clean up text file breaks 
         
if(stristr($this->file["type"], "text")) { 
            
$this->cleanup_text_file($this->file["tmp_name"]); 
         } 
          
         
// get the raw name of the file (without its extenstion) 
         
if(ereg("(\.)([a-z0-9]{2,5})$"$this->file["name"])) { 
            
$pos strrpos($this->file["name"], "."); 
            if(!
$this->file["extention"]) { 
               
$this->file["extention"] = substr($this->file["name"], $posstrlen($this->file["name"])); 
            } 
            
$this->file['raw_name'] = substr($this->file["name"], 0$pos); 
         } else { 
            
$this->file['raw_name'] = $this->file["name"]; 
            if (
$this->file["extention"]) { 
               
$this->file["name"] = $this->file["name"] . $this->file["extention"]; 
            } 
         } 
               if (
move_uploaded_file($this->file["tmp_name"], $this->path $this->file["name"])) { 
                  
$success true
               } else { 
                  
$success     false
                  
$this->error $this->get_error(5); 
               } 
         if(!
$success) { unset($this->file['tmp_name']); } 
         return (bool) 
$success
      } else { 
         
$this->error $this->get_error(3); 
         return 
FALSE
      } 
   } 

    
//Gets the error message if any 
   
function get_error($error_code='') { 
      
$error_message = array(); 
      
$error_code    = (int) $error_code
            
$error_message[0] = "No file was uploaded"
            
$error_message[1] = "Maximum file size exceeded. File may be no larger than " $this->max_filesize/1000 " KB (" $this->max_filesize " bytes)."
            
$error_message[2] = "Maximum image size exceeded. Image may be no more than " $this->max_image_width " x " $this->max_image_height " pixels."
            
$error_message[3] = "Only " str_replace("|"" or "$this->acceptable_file_types) . " files may be uploaded."
            
$error_message[4] = "File '" $this->path $this->file["name"] . "' already exists."
            
$error_message[5] = "Permission denied. Unable to copy file to '" $this->path "'"
        return 
$error_message[$error_code]; 
    } 
}
How can I check more than one mime type or should the actual class be changed to accomadate this.
Reply With Quote
  #2 (permalink)  
Old 07-25-04, 05:04 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
You class doesn't support anything but a string for acceptable types. You will need to change the class.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #3 (permalink)  
Old 07-25-04, 09:49 PM
mikaelf mikaelf is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: php[dot]net
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
This is how your error occurs:
PHP Code:

<?php 


//Your current_file.php
$fu = new FileUpload(); 
    
$fu->max_filesize(5242880); 
    
$fu->max_image_size(15003000); // ($width, $height) 
    //You change $vtypes into array
    
$vtypes = array('image/pjpeg''image/jpjpeg'); 
    
$fu->upload("imagefile"$vtypes".jpg"); 
    
$fu->save_file("uploads/"); 
    if (
$fu->error) { 
       echo 
$fu->error "<br>"
    } else { 
            
// Successful upload! 
            
print($fu->file['name'] . " was successfully uploaded!<br><br>\n"); 
            
// If it's an image, let's display the file 
            
if(stristr($fu->file['type'], "image")) { 
            echo 
"<img src=\"uploads/" $fu->file['name'] . "\" border=\"0\" alt=\"\"><br><br>\n"
            } 
    } 

//Your upload_image.php
class FileUpload 

    var 
$path
    var 
$acceptable_file_types
    var 
$error
    var 
$max_filesize
    var 
$max_image_width
    var 
$max_image_height

    
//Class constructor method 
    
function FileUpload() { 
        
$this->error   ''
    } 
...
   
    
//Checks if file is allowed and uploads to PHP's default directory 
    
function upload($filename=''$accept_type=''$extension='') { 
        
/*Error starts here, you assign $accept_type into array which is disallowed*/
        
$this->acceptable_file_types trim($accept_type); 
...
      
// check to see if the file is of type specified 
     /*This section is executed since $this->acceptable_file_types is not NULL*/
      
if($this->acceptable_file_types) { 
         
/*Condition below is false, since php function stristr cannot evaluate arrays*/
         
if(trim($this->file["type"]) && (stristr($this->acceptable_file_types$this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) { 
            
$this->accepted TRUE
         } else { 
            
$this->accepted FALSE
            
/*Below is code which generates error to your browser*/
            
$this->error $this->get_error(3); 
         } 
      } else { 
         
$this->accepted TRUE
      } 
        
      return (bool) 
$this->accepted
    } 

...

    
//Gets the error message if any 
   
function get_error($error_code='') { 
      
$error_message = array(); 
      
$error_code    = (int) $error_code
            
$error_message[0] = "No file was uploaded"
            
$error_message[1] = "Maximum file size exceeded. File may be no larger than " $this->max_filesize/1000 " KB (" $this->max_filesize " bytes)."
            
$error_message[2] = "Maximum image size exceeded. Image may be no more than " $this->max_image_width " x " $this->max_image_height " pixels."
            
/*Note that str_replace does nothing, because it try to replace something from array and not from string*/
            
$error_message[3] = "Only " str_replace("|"" or "$this->acceptable_file_types) . " files may be uploaded."
            
$error_message[4] = "File '" $this->path $this->file["name"] . "' already exists."
            
$error_message[5] = "Permission denied. Unable to copy file to '" $this->path "'"
        return 
$error_message[$error_code]; 
    } 
}
How to cast away the error:
1. You don't need to change the class.
2. Notice that the class only holds single file upload. Therefore if you have multiple files to be uploaded you have to executed function FileUpload::upload several times as many as your files.
3. Notice that you can only pass string into $vtypes, not array since there is only single file upload. $vtypes could be: image/jpeg, image/gif, image/png or whatever that is prefixed with 'image/'
Reply With Quote
  #4 (permalink)  
Old 07-26-04, 12:56 PM
therat's Avatar
therat therat is offline
Newbie Coder
 
Join Date: Jan 2004
Location: London
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Got the actual MIME types working now. Only need to sort out the multiple uploads part. Any examples appreciated

Last edited by therat; 07-26-04 at 01:27 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bandwith System type thing? videobubba Script Requests 0 06-12-04 03:04 AM
Does anyone know what type of script this site uses phim PHP 4 06-05-04 05:13 PM
blog type script jbkons ASP 0 05-28-04 11:34 AM
Innodb type Table (Foreign key) zoni PHP 1 05-04-04 03:35 AM
Not sure what type of java this is Ikoh JavaScript 2 08-19-03 06:26 PM


All times are GMT -5. The time now is 04:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.