pdf和word文件在php上传

In here i have tried to upload jpeg,pdf ,png and word file formats in php. But i could not able to upload pdf,png and word file formats.but i have successfully upload the jpeg file formats. So this is the code i have tried.please help to edit this code to upload other file formats with out the jpeg file format.

<?php

include_once("db.php");

if(isset($_POST['save'])){

if(($_FILES['file']['type']=='pdf/pdf')
 ||($_FILES['file']['type']=='image/jpeg')
 ||($_FILES['file']['type']=='image/png')
  &&($_FILES['file']['size']<200000))

 {
 if($_FILES['file']['error']>0)
 {

 echo"return code :".$_FILES['file']['error'];
 }
 //else if(file_exists('upload/'.$_FILES['file']['name']))
  //{
  //echo $_FILES['file'] ['name']."Already exite";
  //}
              else if(move_uploaded_file($_FILES['file']         ['tmp_name'],'upload/'.$_FILES['file']['name']))
   {


         $part =$_FILES['file']['name'];
           $sql = mysql_query("INSERT INTO  stu                           (ptype,source,letterno,title,descrip,receiver,image) 
                 VALUES ('{$_POST['pt']}',
                         '{$_POST['so']}',
                         '{$_POST['ln']}',
                         '{$_POST['lti']}',
                         '{$_POST['dic']}',
                         '{$_POST['re']}',
                         '{$part}')");

                            //$sql=  "INSERT INTO  stu (ptype,source, letterno,                     title,descrip,receiver,image) VALUES ('$p', '$s', '$l', '$t','$d','$r','$part')"; 
                                if ($sql){
           echo"jhgjhgjh";
           //echo "successfully insert thise record";
             //echo "<script type='text/javascript'>alert('successfully    insert thise record')</script>";

                     echo "<script            type='text/javascript'>alert('successfully insert thise record')</script>";
         }
        }
          }
         }
             ?>

try changing :

$_FILES['file']['type']=='pdf/pdf'

to:

$_FILES['file']['type']=='application/pdf'

for doc type:

$_FILES['file']['type']=='application/msword'

for docx:

$_FILES['file']['type']=='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  • Note: also check if the file size of the file that you're trying to upload is not greater than allowed.
  1. PDF documents have different MIME type: application/pdf, you should use it.
  2. Microsoft Word documents MIME type application/msword is not in list of your allowed mime types, so it is failed to get through your check.
  3. You have size limit of 200kib, it is about 195kb, probably you have documents larger then this (pretty low) limit.
  4. After raising local file limit - check your upload_max_filesize setting in php.ini since it is next size limitation boundary, you may stuck on.

Besides this your code have logical problem into MIME type checking condition since you didn't group MIME checks into separate logical value. Your condition is actually: if (a OR b OR c AND d) while is should be if ((a OR b OR c) AND d).

It will be even better to rewrite this code as:

$allowed_mime_types = array('image/png','image/jpeg','application/pdf','application/msword');
$size_limit = 200000;
if ((in_array($_FILES['file']['type'], $allowed_mime_types)) && ($_FILES['file']['size'] < $size_limit)) { ... }