uploadform.html和upload_file.php(如下)在 Firefox 中运行良好,但在上传 ASCII .stl 3D 文件时在 Chrome、IE 和 Safari 中失败。错误消息是“无效文件”,多台计算机和多个 .stl 文件出现问题。当我修改代码以支持其他文件类型(如 JPG 和 PDF)时,它允许在所有四种 Web 浏览器中使用这些文件类型。此外,如果我在 mime 类型部分中包含 application/octet-stream,Firefox 仅允许上传 .stl。为什么这在 Firefox 之外不起作用?
上传表单.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>
上传文件.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>