How to Upload MIME Types With PHP
MIME stands for Multipurpose Internet Mail Extensions. It is an Internet media file type that is part of a two-part identifier of file formats used on the Internet. There are several different MIME types of files, including the SMTP, HTTP and SIP Internet protocols. PHP is a scripting language used to create dynamic content websites. When talking about uploading MIME types with PHP, the most common types are the video and graphics MIME type files; GIF, JPEG, PNG, SVG, TIFF, ICO, MPEG, MP4, QuickTime and Windows Media Video are common MIME types uploaded and used on websites.
Instructions
-
-
1
Open up a blank page in Notepad or another text editing program. If you prefer to use a Web authoring application like Dreamweaver, that is fine also. Just open the program in "Code" view and not WYSIWYG (What You See is What You Get) view.
-
2
Type in the code for the form that will be the user interface for the upload.
""<html>
<head>
<body>
<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose the MIME type file to upload (non-MIME type files are allowed): <input name="filename" type="file" /><br />
<input type="submit" value="Upload MIME File" />
</form>"
-
-
3
Copy and paste the following code below the code for the form:
"<?php
$upload_success = FALSE;
//MIME image and video files are large files so we have to specify a large maximum file size
define ('MAX_FILE_SIZE', 1024000);
if (array_key_exists('upload', $_POST)) {
// this is the folder where you will save the uploaded files; remove the brackets
define('UPLOAD_DIR', '{specify a folder on your server to save the files to'};
// when successfully detected upload
$upload_success = TRUE;
// change the filename in case files with the same name is being uploaded to the same folder
$filename = str_replace(' ', '_', $_FILES['image']['name']);
// convert the filesize from MB to KB
$max_size = number_format(MAX_FILE_SIZE/1024, 1).'kb';
// This is the array that will allow the permitted MIME types; remove the ones you don't want to allow or add other MIME types
$permitted_files = array('image/jpeg','image/pjpeg','image/gif','image/tiff','image/ico','image/png','image/svc','video/mov','video/mpeg4','video/mp4','video/wmv' );
// set the variables to false so that the code can check it and make sure it is acceptable
$sizeOK = false;
$typeOK = false;
//make sure that the file is larger than the maximum size permitted
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// make sure that the file is of a permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
$display_error = "File not uploaded successfully. Only MIME types are allowed; ". "please upload an acceptable ";
print $display_error
}
}
?>
</body>
</head>
</html>"
-
4
Save the file with the "PHP" file extension and upload it to your server.
-
1
References
- Photo Credit upload buton image by Attila Toro from Fotolia.com