How to Upload MIME Types With PHP

How to Upload MIME Types With PHP thumbnail
Upload MIME files to a Web server using 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.

Related Searches:

References

  • Photo Credit upload buton image by Attila Toro from Fotolia.com

Comments

You May Also Like

  • PHP File Upload Types

    PHP File Upload Types. Before Web 2.0, PHP revolutionized web development by simplifying the process of using uploaded files. Despite this drastic...

  • PHP Image Mime Types

    PHP Image Mime Types. Originally developed for e-mail usage, Multipurpose Internet Mail Extensions (MIME) have become the standard method across the web...

  • How to Create Plain Text & Mime Formatted Email With PHP

    When sending emails, there are two different formats used: plain text and MIME format. MIME is short for Multipurpose Internet Mail Extensions....

  • JPEG Mime Types

    JPEG Mime Types. A MIME type is a formally agreed-upon standard for describing content to be shared over the Internet, specifically through...

  • Facts on MIME Format

    Sending e-mail is pretty much a fact of life for most people. Many are unaware of the limitations that the ASCII-based messages...

  • How to Upload Files to a Web Server in PHP

    You want a website that allows visitors to upload files and interact with your site. PHP is a scripting language you can...

  • How to Upload a CSV File to PHP

    The PHP scripting language allows you to execute functions on a web server. One of the things that you can do with...

  • How to Upload & Resize a PHP MySQL Image

    When dealing with a large amount of images, it is often useful to store them in a database. This makes accessing and...

  • Web Server MIME Types

    Web Server MIME Types. The term MIME types refers to a set of standard designations established by the Internet Engineering Task Force...

  • How to Register a MP4 Mime Type in IIS

    Multipurpose Internet Mail Extensions (MIME) types instruct the user's Web browser to open a specific program for a file extension. MIME types...

Related Ads

Featured