0

I am trying to upload a PDF file, but getting the "Invalid File" error. I have used the code from w3schools, but its not working. Here is the PHP code

    $allowedExts = array("pdf");
    $temp = explode(".", $_FILES["pdfFile"]["name"]);
    $extension = end($temp);
    if ((($_FILES["pdfFile"]["type"] == "application/pdf"))
    && ($_FILES["pdfFile"]["size"] < 1200000)
    && in_array($extension, $allowedExts))
      {
      if ($_FILES["pdfFile"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["pdfFile"]["error"] . "<br>";
        }
      else
        {
        echo "Upload: " . $_FILES["pdfFile"]["name"] . "<br>";
        echo "Type: " . $_FILES["pdfFile"]["type"] . "<br>";
        echo "Size: " . ($_FILES["pdfFile"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["pdfFile"]["tmp_name"] . "<br>";

        if (file_exists("../assets/pdf/" . $_FILES["pdfFile"]["name"]))
          {
          echo $_FILES["pdfFile"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["pdfFile"]["tmp_name"],
          "../assets/pdf/" . $_FILES["pdfFile"]["name"]);
          echo "Stored in: " . "../assets/pdf/" . $_FILES["pdfFile"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
3
  • Can you post what you have in <form> section from your HTML Commented Mar 31, 2014 at 11:24
  • First var_dump($_FILES["pdfFile"]). There could be 3 problems: 1. type != "application/pdf" 2. File size > 1200000 3. Extension not allowed. I think you forgot to set the enctype on the form. Commented Mar 31, 2014 at 11:26
  • You should have enctype="multipart/form-data" in form tag to upload a file Commented Mar 31, 2014 at 11:31

2 Answers 2

1

Could you just check (by echo, var_dump or whatever suits you) the content of $_FILES["pdfFile"] ? Are you sure it fits ?

And you did set the enctype of Your upload form as well ? http://www.w3schools.com/tags/att_form_enctype.asp

And the form method is equal POST ?

Sign up to request clarification or add additional context in comments.

2 Comments

In this case You are uploading the file with diffrent name, just check content of $_FILES array and You will get Your answer easly. Next just swap the 'pdfFile' with the new name and Your good to go.
@NiteshYadav Check if the file element name is same as what you have in PHP i.e. pdfFile
0

Could this be a problem with file permissions?

Check if you have permission to write to folder ../assets/pdf/.

3 Comments

He dont even reach that part of code so how can this be a problem ?
You're right. It's hard to see with this indent and improper error checking.
Yes no offence just want to make all the doubts cleared. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.