user3124850
user3124850

Reputation: 71

how to upload doc and docx in php?

Am trying to upload document and docx in php through html input but am unable to upload doc and docx even i mentioned the mime type correctly.please help me out to upload doc and docx.i wrote my code above..thanks in advance..

<?php

$allowedExts = array(".doc", ".docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& 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"] / 200000) . " 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";
 }

Upvotes: 1

Views: 19349

Answers (5)

Mohammad Ali Abdullah
Mohammad Ali Abdullah

Reputation: 331

  $docx = $_FILES['file']['name'];
  $docxTmp = $_FILES['file']['tmp_name'];
  $docxSize = $_FILES['file']['size'];
  $type = $_FILES["file"]["type"];
  if ($docx) {
    $file_dir = '../upload/';
    $allowedExts = array("doc", "docx");
    if (($type == "application/msword" || $type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && in_array($extension, $allowedExts)) {
      move_uploaded_file($docxTmp, $file_dir . $docx);
    }
  }

Upvotes: 0

hizbul25
hizbul25

Reputation: 3849

Try with this :

<?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: 0

Mohd Sadiq
Mohd Sadiq

Reputation: 191

Here is The correct code

$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < 2000000)
&& 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"] / 200000) . " 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";
 }

Upvotes: 1

Haim Evgi
Haim Evgi

Reputation: 125496

2 fixes :

1 . change extension array

$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");

2 . to allow docx change the doubled line to

|| ($_FILES["file"]["type"] 
    == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")

Upvotes: 3

Peon
Peon

Reputation: 8020

You don't need the . for doc and docx

$allowedExts = array("doc", "docx", "pdf", "gif", "jpeg", "jpg", "png");

Upvotes: 0

Related Questions