Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Upload form, PDF, Doc & Docx

Tags:

html

php

upload

I'm struggling to make this upload code work for a docx file, it works okay for doc and pdf..

$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
 {
  if ($_FILES["file"]["error"] > 0)
 {
   echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
 }

this is part of a project from a while ago and i honestly can't remember how to do it..

I know it's not the most secure upload method, but if someone could help it would be appreciated!

I'm thinking i need to add another line here:

if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)

Just not sure what.. Help is appreciated!

Edit: So i've got to this stage (with the help of comments!)

$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
//if ((($_FILES["file"]["type"] == "application/pdf")
//|| ($_FILES["file"]["type"] == "application/msword"))
if (($_FILES["file"]["type"] == "application/pdf") 
|| ($_FILES["file"]["type"] == "application/msword") 
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-    officedocument.wordprocessingml.document"))
&& ($_FILES["file"]["size"] < 20000000)

&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {

But now its coming up with: Parse error: syntax error, unexpected T_BOOLEAN_AND in /var/sites/s/stanation.com/public_html/forms/process/insert.php on line 30

like image 638
Ali Clarke Avatar asked Dec 08 '22 12:12

Ali Clarke


1 Answers

For docx check this MIME type

application/vnd.openxmlformats-officedocument.wordprocessingml.document

EDIT :

Here's the code . You're missing parenthesis

<?php

    $allowedExts = array("pdf", "doc", "docx");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
    {
      if ($_FILES["file"]["error"] > 0)
      {
         echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
      }
      else
      {
        echo "Success";
      }
  }
like image 109
Shankar Narayana Damodaran Avatar answered Dec 29 '22 16:12

Shankar Narayana Damodaran