Question:
PHP file upload - text?
anonymous
2009-04-26 09:36:14 UTC
writing some code that allows you to upload files to the server (im using a tutorial) and i know i can just remove the check but i dont want anything like an .exe uploaded

this allows jpegs to be uploaded

($_FILES["file"]["type"] == "image/jpeg")

but i want to upload a .txt file and a .xml file and cant figure out what the correct code is.

anyone know ?

thanks
Four answers:
Nigel A
2009-04-26 10:04:03 UTC
This is called a mime-type



.txt= "text/plain"

.xml = "text/xml"



You can find out the code for any file type by echoing the type after upload



echo $_FILES["file"]["type"];



Or you can look up a list

http://www.webmaster-toolkit.com/mime-types.shtml



To implement this in PHP:



$allowed=array("text/plain"

,"text/xml"

,"image/jpeg");



if (in_array(

$_FILES["file"]["type"]

,$allowed))

{

echo "It's good";

// process the file

}

else

{

die('File type not allowed.');

}



Excuse the line breaks, yahoo truncates otherwise.
?
2009-04-26 16:51:13 UTC
To find out how a "type" is received or detected, I use a simple (but effective) way: I display the file['type']... :-)

Make a piece of code to upload your file, and the receiving nd display what filetype arrives.

Define your tests from there.
anonymous
2009-04-27 20:03:10 UTC
here is a mysql table with mime types and a snippet of code to test if the mime type is allowed





--

-- Table structure for table `mime_types`

--



DROP TABLE IF EXISTS `mime_types`;

CREATE TABLE IF NOT EXISTS `mime_types` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`extension` varchar(5) NOT NULL,

`type` varchar(100) NOT NULL,

`aud_vid` int(11) NOT NULL,

`access_type` int(1) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;



--

-- Dumping data for table `mime_types`

--



INSERT INTO `mime_types` (`id`, `extension`, `type`, `aud_vid`, `access_type`) VALUES

(1, '.au', 'audio/basic', 1, 1),

(2, '.avi', 'application/x-troff-msvideo', 1, 0),

(3, '.bm', 'image/bmp', 0, 1),

(4, '.bmp', 'image/bmp', 0, 1),

(5, '.doc', 'application/msword', 0, 0),

(6, '.flp', 'application/FruityLoops Project', 1, 1),

(7, '.gif', 'image/gif', 0, 1),

(8, '.gz', 'application/x-gzip', 0, 0),

(9, '.gzip', 'application/x-gzip', 0, 0),

(10, '.jpe', 'image/jpeg', 0, 1),

(11, '.jpeg', 'image/jpeg', 0, 1),

(12, '.jpg', 'image/jpeg', 0, 1),

(13, '.kar', 'audio/midi', 1, 1),

(14, '.mid', 'audio/midi', 1, 1),

(15, '.mov', 'video/quicktime', 1, 0),

(16, '.mp2', 'audio/mpeg', 1, 1),

(17, '.mp3', 'audio/mpeg3', 1, 1),

(18, '.mpa', 'audio/mpeg', 1, 1),

(19, '.mpe', 'video/mpeg', 1, 1),

(20, '.mpeg', 'video/mpeg', 1, 0),

(21, '.mpg', 'video/mpeg', 1, 0),

(22, '.pdf', 'application/pdf', 0, 0),

(23, '.png', 'image/png', 0, 1),

(24, '.ppt', 'application/mspowerpoint', 0, 0),

(25, '.qt', 'video/quicktime', 1, 0),

(26, '.ra', 'audio/x-pn-realaudio', 1, 1),

(27, '.ram', 'audio/x-pn-realaudio', 1, 1),

(28, '.rmi', 'audio/mid', 1, 1),

(29, '.rtx', 'application/rtf', 0, 0),

(30, '.swf', 'application/x-shockwave-flash', 0, 0),

(31, '.tif', 'image/tiff', 0, 1),

(32, '.tiff', 'image/tiff', 0, 1),

(33, '.txt', 'text/plain', 0, 0),

(34, '.wav', 'audio/wav', 1, 1),

(35, '.wma', 'audio/x-ms-wma', 1, 1),

(36, '.wmv', 'video/x-ms-wmv', 1, 0),

(37, '.xls', 'application/excel', 0, 0),

(38, '.zip', 'application/zip', 0, 0),

(39, '.mp3', 'audio/x-mpeg', 1, 1),

(40, '.ppt', 'application/vnd.ms-powerpoint', 0, 0);





//Check to see if MIME is authorized

$this->type = $_FILES['file_to_upload']['type'];

$this->query_string = "SELECT * FROM mime_types WHERE type='$this->type'";

$this->query = mysql_query($this->query_string);

$this->num_rows = mysql_num_rows($this->query);

if($this->num_rows == "")

{

$this->file_form($UserInput);

echo "
File type not allowed!
or
You did not select a file to upload!
Contact support! or $this->goback
";

$this->Error == true;

exit;

}

else

{

$this->mime_info = mysql_fetch_assoc($this->query);

$UserInput['file_access'] = $this->mime_info['access_type'];

}
EFG
2009-04-26 17:02:29 UTC
text/plain



text/xml


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...