Reputation: 3
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?
Upvotes: 0
Views: 7515
Reputation: 56412
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.
Upvotes: 2