Skip to main content
Community Expert
September 23, 2012
Question

Php File upload script fails to recognize file types.

  • September 23, 2012
  • 1 reply
  • 835 views

I had this script working for quite a while, then made some changes to another part of the page. I must have inadvertently changed a line or moved something here. I've been looking at the code and trying different things for a few hours now. I'm just running around in circles.

Maybe a fresh pair of eyes will see something that I'm missing. I'd appreciate a look. No matter what I do the upload never begins and I get an error reported. It's like the $allowedExts and the file size is completely ignored and the last error code is displayed. I'm just not seeing the error in the code.

Here's the php:

<?php

// --------------- save uploaded file ----------------

$allowedExts = array("txt","csv", "css","doc","xls","rtf","ppt","pdf","swf","flv","avi", "wmv","mov","jpg","jpeg","gif","png", "dwg");

$extension = end(explode(".", $_FILES["file"]["name"]));

if (($_FILES["file"]["type"] > "")

&& ($_FILES["file"]["size"] < 20000000)

&& in_array($extension, $allowedExts))

  {

  if ($_FILES["file"]["error"] > 0)

    {

    echo "Return Code: " . $_FILES["file"]["error"];

    }

  else

    {

    if (file_exists("../pvtDocs/" . $_FILES["file"]["name"]))

      {

      echo '<input name="mstrLink" type="text" style="font-weight:bold; background:orange; color:white" id="mstrLink" value="File already exists, Try Again" size="80" />';

      }

    else

      {

      move_uploaded_file($_FILES["file"]["tmp_name"],

      "../pvtDocs/" . $_FILES["file"]["name"]);

      echo '<input name="mstrLink" type="text" id="mstrLink" value="'. $_FILES["file"]["name"]. '" size="80" />';

      }

    }

  }

else

  {

      echo '<input name="mstrLink" type="text" style="font-weight:bold; background: red; color:white" id="mstrLink" value="Invalid File" size="80" />';

  }

?>

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
September 30, 2012

Did you by any chance change the name of the file field? It sounds as though $_FILES["file"] is an empty array.