PHP - how to restrict upload to pdf, doc etc - Asked By Olusegun A. on 29-Sep-09 04:41 PM

Hello there,

I am trying to restrict my upload to pdf, and microsoft word document. i tried to edit the jpeg upload syntax but  the script is echoing error, please how do i do it.

how to restrict upload to pdf, doc etc in PHP - mv ark replied to Olusegun A. on 29-Sep-09 08:14 PM

To restrict files of only certain extensions, you can first validate with JavaScript & then on the server-side with PHP

For client-side, validation you can use the jQuery Validation plugin -
http://docs.jquery.com/Plugins/Validation/Methods/accept#extension
or
try this snippet - http://stackoverflow.com/questions/651700/how-to-have-jquery-restrict-file-types-on-upload

You can then check for the file's MIME content & extension in PHP as explained here - http://hungred.com/2009/08/17/useful-information/secure-file-upload-check-list-php/

Three ways. - [)ia6l0 iii replied to Olusegun A. on 29-Sep-09 10:07 PM

The easiest way is to check the file mime type and restrict. Like,

if (($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/pdf")){
//file is fine
}else{
echo "this type of file cannot be uploaded";}

Or you could use Regular expressions to do a match, Like

$allowed = array(".doc", ".pdf");
foreach ($allowed as $file) 
{
if(!preg_match("/$file\$/i", $_FILES['userfile']['name'])) 
{
echo "this type of file cannot be uploaded";
}
}

And you could use the first five characters of the file to check if it is a valid pdf file.

$head = fgets(fopen("/path/to/file.pdf", "r"), 5);

if($head == '%PDF-') 
{
  
}
else
{
  echo "this type of file cannot be uploaded";
}

Note: Watch out for syntax errors.

upload only .jpeg file using php - DL M replied to Olusegun A. on 30-Sep-09 01:02 AM

you can use this code if you want to upload only .jpeg file

if($_FILES['file']['type'] == "image/pjpeg" || $_FILES['file']['type']=="image/jpeg" || $_FILES['file']['type']=="image/gif") {
   //do something
} else {
  //wrong extension
}
Vasanthakumar D replied to Olusegun A. on 30-Sep-09 02:08 AM

hi,

here is the script validation for this...


<script type="text/javascript"  language="javascript">
       function CheckExtenstion(con)
       {
            if(con.value != "")
            {
                if(con.value.indexOf(".") > -1)
                {
                    var ext = con.value.substring(con.value.lastIndexOf(".") + 1);
                    if(ext.toLowerCase() == "doc" || ext.toLowerCase() == "pdf")
                    {
                        
                    }
                    else
                    {
                        alert('Please select doc or pdf only');
                    }
                }
                else
                {
                    alert('Please enter valid file');
                }
            }
       }
    </script>


and call the script as

 <input id="File1" type="file" onchange="CheckExtenstion(this);"/>
Help with restricting files - Jason Deal replied to [)ia6l0 iii on 12-Nov-09 11:47 AM

Hello, I had the same question the original poster asked so I thought I'd ask it here. I'd like to restrict what files can be uploaded to .doc and .pdf. I tried your scripts and none of them seem to work. I've tried all kinds of restricting file scripts and I either get a syntax error or the script doesn't work and still lets me upload any type of file. I'm pretty new to php...can you tell me what I'm doing wrong? Here's what I have:


<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if (($_FILES["file"]["type"] == "application/msword")|| ($_FILES["file"]["type"] == "application/pdf")){ //file is fine}else{ echo "this type of file cannot be uploaded";}
{
  move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
    "C:/upload/" . $_FILES["fileToUpload"]["name"]);
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
  }
 
Here's the upload form on the html page:

<form enctype="multipart/form-data" action="upload2.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" <input id="File1" type="file"/>
</form>

Thanks in advance for your help!