Reputation: 207
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
Upvotes: 4
Views: 48159
Reputation: 3055
There is another way to do the task. Just check the file type and then you can work further.
Here is code to check the filetype.
$target_dir = "uploads/";
$filename= $_FILES["fileupload"]["name"]; //gets filename with type
$target_file = $target_dir . basename($filename); //uploads/file.type
echo $target_file;
$extension= pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType = strtolower($extension);
if(strcmp($imageFileType,"docx")==0){
echo "Its word file";
}
Upvotes: 0
Reputation: 1
This following check will help you to upload .docx
files:
$_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Upvotes: 0
Reputation: 68476
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";
}
}
Upvotes: 14