Freshman
Freshman

Reputation: 303

How to upload odt/doc/pdf/txt files with php

I can't get my uploadscript to work for the fileformates pdf, doc, odt and txt. Is there a way to accept all fileformates, just with limmited maxsize on the object? Here is my script:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png", "doc", "odt", "pdf", "txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_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"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/txt")
|| ($_FILES["file"]["type"] == "application/doc")
|| ($_FILES["file"]["type"] == "application/odt"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Feilmelding: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Denne filen er lastet opp: " . $_FILES["file"]["name"] . "<br>";
    echo "Type fil:: " . $_FILES["file"]["type"] . "<br>";
    echo "Størrelse: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " Filen eksisterer, bytt navn. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Filen er lagret her: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Feil Filtype";
  }
?>

It works for the pictures, but not the applications. Any help would be most appreciated, have a great eve

In advance, thank you :)

Upvotes: 3

Views: 19348

Answers (1)

&#201;tienne Miret
&#201;tienne Miret

Reputation: 6650

You use incorrect media types. The correct media-types are:

  • application/msword for .doc
  • text/plain for .txt
  • application/vnd.oasis.opendocument.text for .odt
  • application/pdf for .pdf

Upvotes: 6

Related Questions