Php 上载表单仅在Firefox中上载ASCII.stl 3D文件时有效

Php 上载表单仅在Firefox中上载ASCII.stl 3D文件时有效,php,forms,post,mime-types,Php,Forms,Post,Mime Types,uploadform.html和upload_file.php(如下)在Firefox中工作正常,但在Chrome、IE和Safari中上载ASCII.stl 3D文件时失败。错误消息为“无效文件”,多台计算机和多个.stl文件出现问题。当我修改代码以支持其他文件类型(如JPG和PDF)时,它允许在所有四种web浏览器中使用这些文件类型。另外,Firefox只允许在mime类型部分包含application/octet流时上传.stl。为什么这在Firefox之外不起作用 uploadform.

uploadform.htmlupload_file.php(如下)在Firefox中工作正常,但在Chrome、IE和Safari中上载ASCII.stl 3D文件时失败。错误消息为“无效文件”,多台计算机和多个.stl文件出现问题。当我修改代码以支持其他文件类型(如JPG和PDF)时,它允许在所有四种web浏览器中使用这些文件类型。另外,Firefox只允许在mime类型部分包含application/octet流时上传.stl。为什么这在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>
<!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>

文件名:

upload\u file.php:

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

尝试设置IE的编码属性

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


解决方案:
应用程序/netfabb
添加到允许的mime类型部分。这是大多数浏览器分配给.stl文件的mime类型。Firefox是一个例外,它将.stl文件标识为
application/octet stream

Chrome/IE中的$\u文件[“file”][“type”]包含哪些内容?这并没有解决问题@ShapeShifter如果您回显$_文件[“文件”][“类型”];它将返回什么。它响应以下内容:application/netfabbSolution:将application/netfabb添加到允许的mime类型部分。非常感谢@shapeshifter为我指明了这个方向。太好了!根据it的声音,应用程序/八位字节流是未知内容类型的默认值。从安全的角度来看,允许八位组流与允许任何事情都是一样的!看见