Wednesday, August 31, 2011

Restrict the file uploads using PHP


Below is the php code to restrict the file uploads for a particular type of files. As an example, I'm showing all the extension types in the "Allowed extensions" variable.


<?php

  $allowedExtensions = array("txt","csv","htm","html","xml",
    "css","doc","xls","rtf","ppt","pdf","swf","flv","avi",
    "wmv","mov","jpg","jpeg","gif","png");
    if ($_FILES['upload_file']['tmp_name'] > '') {
      if (!in_array(end(explode(".",
            strtolower($_FILES['upload_file']['name']))),
            $allowedExtensions)) {
       die($_FILES['upload_file']['name'].' is an invalid file type!<br/>'.
        '<a href="javascript:history.go(-1);">'.
        '&lt;&lt Go Back</a>');
      }
    }

?>





2 comments:

dskanth said...

Good information Anil...

Anil Kumar said...

Thanks shashi :) ...let me know if anythings more that I can post which will be helpful for us.....