upload form only works in Firefox when uploading ASCII .stl 3D files

Posted by NathanPDX on Stack Overflow See other posts from Stack Overflow or by NathanPDX
Published on 2012-11-13T04:49:15Z Indexed on 2012/11/13 4:59 UTC
Read the original article Hit count: 181

Filed under:
|
|
|

uploadform.html and upload_file.php (below) works fine in Firefox but fails in Chrome, IE, and Safari when uploading ASCII .stl 3D files. Error message is "Invalid file" and problem occurs with multiple computers and multiple .stl files. When I modify the code to support other file types like JPG and PDF it allows those file types in all three web browsers. Also, Firefox only allows the .stl upload if I include application/octet-stream in the mime types section. Why doesn't this work outside of Firefox?

uploadform.html:

<!doctype html>
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

upload_file.php:

<!doctype html>
<html>
<body>
<?php
$allowedExts = array("stl");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (
        (
   ($_FILES["file"]["type"] == "application/sla")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "application/unknown")
        )
&& ($_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 "Size: " . ($_FILES["file"]["size"] /1024) . " KB<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 "successful upload";
      }
           }
}
else
        {
    echo "Invalid file";
        }
?>
</body>
</html>

© Stack Overflow or respective owner

Related posts about php

Related posts about forms