1

I have a form that aims to upload pdf's, MSWord docs, MSExcel files, and MSPowerpoint.

I'm tying to do a PHP validation as follows

<?php
if (isset($_FILES['fileupload'])){

  $filetype = array('application/pdf', 'application/msword', 'application/mspowerpoint');
  if (in_array($_FILES['fileupload']['type'], $filetype)){
    if(move_uploaded_file($_FILES["fileupload"]["tmp_name"], "/var/www/uploads/" . $_FILES["fileupload"]["name"])){
        $filename = basename($_FILES['fileupload']['name']);
        echo $filename;
      }//close moveupload
    }//close in_array
  }//close isset
}

?>

Why am I not able to upload pdf-like files but can upload MSword-like files? I have seen similar questions like this but I want to know why for this particular case it is not working for me.

Community
  • 1
  • 1
Maina Mailu
  • 73
  • 3
  • 13

2 Answers2

2

give case of whetever file type you want to upload and enjoy

 $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['file']['type']);
    switch ($mime) {

       case 'application/pdf':

       default:
           die("Unknown/not permitted file type");
    }
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0

This is my test code:

<?php 
ob_start("ob_gzhandler");
header('Content-Type: text/html; charset=utf-8');
  $filetype = array('application/pdf', 'application/msword', 'application/mspowerpoint');
  $type = $_FILES['fileupload']['type'];
  if (in_array($type, $filetype)){
    if(move_uploaded_file($_FILES["fileupload"]["tmp_name"], $_FILES["fileupload"]["name"])){
        $filename = basename($_FILES['fileupload']['name']);
        $show .= '<h2>saved sucessfully</h2>';
      }//close moveupload
    }//close in_array
    else{
      $show .= '$type = ' . $type . "\n" ;
      $show .= "\nfile type: " . var_export($filetype,true). "\n";
      $show .= "\n  ###";
    }


echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Test Bed</title></head></body><pre>
$show
</pre>
in_array
application/pdf
application/pdf
<form action="#" method="post" enctype="multipart/form-data">
<input type="file" name="fileupload" />
<button>Submit</button>
</form></body></html>

EOT;
?>

Results


saved sucessfully

For uploads without the multiple set:

  if ($_FILES["fileupload"]["error"] == UPLOAD_ERR_OK) {
    if (!in_array($_FILES['fileupload']['type'], $filetype)){
      $tmp_name = $_FILES["fileupload"]["tmp_name"];
      $filename = basename($_FILES["fileupload"]["name"]);
      move_uploaded_file($tmp_name, $filename);
    }
  }
}
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
  • i'm doing single file upload not multiple files. Your solution seems to be geared towards multiple file uploads – Maina Mailu May 07 '15 at 13:38
  • have copy-pasted the code as-is but still not working – Maina Mailu May 07 '15 at 13:56
  • it's in the in_array, I added an else, but I have not yet found out why `in_array()` is not working. I do not think there was any difference between mine and yours. – Misunderstood May 07 '15 at 14:09
  • I have run your test code....it returns 0 error but pdf still does not upload. The in_array in yours has a ! not operator preceding it – Maina Mailu May 07 '15 at 14:17
  • It works perfectly...i had made a spelling mistake instead of $filename i put $filname....excellent support @Misunderstood. – Maina Mailu May 07 '15 at 14:20
  • I just put my final test version. No changes except I output something when the file was saved. I had to change your path on the ` move_uploaded_file`. It usually is, more times than not, some little thing like that. – Misunderstood May 07 '15 at 14:25