0

I want to upload a file(pdf ) using php,

I found a code in the internet and I just add the type of my files : application/pdf but it didn't work for the pdf file . (it works fine for the images).

html code

<form action="upload_file.php" method="post"
 enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file"><br>
 <input type="submit" name="submit" value="Submit">
 </form>

the php code

$allowedExts = array("gif", "pdf", "GIF", "jpeg", "JPEG", "jpg", "JPG", "png", "PNG");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
 || ($_FILES["file"]["type"] == "application/pdf")
 || ($_FILES["file"]["type"] == "image/jpeg")
 || ($_FILES["file"]["type"] == "image/jpg")
 || ($_FILES["file"]["type"] == "image/pjpeg")
 || ($_FILES["file"]["type"] == "image/x-png")
 || ($_FILES["file"]["type"] == "image/png"))
 && ($_FILES["file"]["size"] < 90000000000) // increased allowed size may be your problem
 && in_array($extension, $allowedExts))
{
 if ($_FILES["file"]["error"] > 0)
 {
 echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
 }
 else
 {
 echo "Upload: " . $_FILES["file"]["name"] . "<br>";
 echo "Type: " . $_FILES["file"]["type"] . "<br>";
 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
 if (file_exists("upload/" . $_FILES["file"]["name"]))
 {
 echo $_FILES["file"]["name"] . " already exists. ";
 }
 else
 {
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
 }
 }
}
else
{
 echo "Invalid file";
}

the error is

Invalid file

*

Update

*

after adding var_dump($_FILES['file']); at the begining of my script

On chrome (works fine ) the output is:

array
 'name' => string 'Nouveau Document Microsoft Office Word.pdf' (length=42)
 'type' => string 'application/pdf' (length=15)
 'tmp_name' => string 'C:\wamp\tmp\php58AD.tmp' (length=23)
 'error' => int 0
 'size' => int 79770
Upload: Nouveau Document Microsoft Office Word.pdf
Type: application/pdf
Size: 77.900390625 kB
Temp file: C:\wamp\tmp\php58AD.tmp

on firfox it didnt work the output is

array
 'name' => string 'Nouveau Document Microsoft Office Word.pdf' (length=42)
 'type' => string 'application/force-download' (length=26)
 'tmp_name' => string 'C:\wamp\tmp\phpBEFD.tmp' (length=23)
 'error' => int 0
 'size' => int 79770
string 'application/force-download' (length=26)

Update2

*The code wirthout $_FILES["file"]["type"]*

 var_dump($_FILES["file"]["type"]);
 $allowedExts = array("gif","pdf", "GIF");
 $extension = end(explode(".", $_FILES["file"]["name"]));
 in_array($extension, $allowedExts);
 echo "Upload: " . $_FILES["file"]["name"] . "<br>";
 echo "Type: " . $_FILES["file"]["type"] . "<br>";
 echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
 echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

it worked fine for all my browsers but I have to test the type and size of the files! and what was the problem?

asked May 23, 2013 at 15:21
3
  • 3
    Try to add var_dump($_FILES["file"]["type"]); right after the invalid file echo. Commented May 23, 2013 at 15:23
  • You are doing a case-sensitive search (not a very good idea...) and you did not add PDF to the allowed extensions array. Does your test file have a lower-case extension? Commented May 23, 2013 at 15:28
  • @jeroen yes my test file has lower-case extension Commented May 24, 2013 at 9:04

1 Answer 1

2

Be aware that file mime type is send by client, not detected by server. Thus, it can be easily fooled.

You shouldn't check that. Remove any check about it, and just check the file extension.

You may want to change this line :

 $extension = end(explode(".", $_FILES["file"]["name"]));

With that :

$extension = strtolower(end(explode(".", $_FILES["file"]["name"])));

And change your array with just lowercases extensions.

answered May 23, 2013 at 15:28
Sign up to request clarification or add additional context in comments.

4 Comments

You may want to var_dump($_FILES['file']) at the begging of your script, so you can see what's going on, and what's the difference between the two browsers.
Would you be kind enough to update your code in your post with the last modifications ? Like tests about mimetype removed.
as I have posted string 'application/force-download' (length=26) is the error even with adding and removing mimetype.
It's not an error, it's a trace, to help you debug. Can you please post updated code ? You should have removed every line containing $_FILES["file"]["type"].

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.