Hi all,
t to upload files to my server. Here i have created a coding. This only allows me to upload pictures. How can i amend my coding to upload word and excel files also...

<?php

$db=mysql_connect('localhost','root','');
mysql_select_db('bank');

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000))
  {
  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";
  }
?>

To upload pdf, word, excel files you need to add the files types of the files like below,

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel"))
&& ($_FILES["file"]["size"] < 20000000))

I have entered them but still when i trying to upload an excel, word or pdf file an error displayed as "invalid file"

debug!
instead of jumping in and doing a bunch of processing...

comment this out for now, run it on a test page...
/*
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000))
*/
/*
 and start uploading files start with a pdf and see what comes across in the type, and then as paulraj suggested
echo $_FILES["file"]["type"];
continue to add these types in the 'acceptable list' that you get back from your echo.
*/
|| ($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel"))

Thanks both of you.....It works for a PDF file.

$_FILES["file"]["type"] is supplied by the browser and is not checked by php for accuracy. You can overcome this by using something like the Fileinfo extension to actually check the file's mime type (http://www.php.net/manual/en/book.fileinfo.php)

You could drastically simplify the way that code reads by supplying an array of file types and then checking if the provided is in that array. e.g:

$allowed = array('image/gif','image/jpeg','image/pjpeg','application/pdf','application/msword','application/vnd.ms-excel');

if ( in_array($_FILES["file"]["type"], $allowed) && $_FILES["file"]["size"] < 20000000 )
{...}

Also if you're going to match by the mime you should consider normalizing the value that is provided. At the minimum make it's case consistent e.g. $type = strtolower($_FILES["file"]["type"]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.