简体   繁体   中英

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

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 four 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>

Solution: add application/netfabb to the section of allowed mime types. This is the mime type most browsers assign to .stl files. Firefox is an exception and identifies .stl files as application/octet-stream .

尝试为IE设置编码属性。

 <form action="upload_file.php" method="post" enctype="multipart/form-data" encoding="multipart/form-data" >

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM