正在将 excel 个文件上传到服务器
Uploading excel file to server
我无法将 msoffice 文件上传到服务器上的文件夹,我可以上传 .pdf 和图片文件,但不能上传 .docx、.xlsx 甚至 .txt,从我有限的语法来看,我的所有语法看起来都是正确的知识。这是表格
<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="fileSelect">Navigate and choose:</label>
<input type="file" name="file" id="fileSelect"><br><br>
<input class="button" type="submit" name="action" value="Upload to Shared Folder">
</form>
这是 upload_file.php
if(isset($_FILES["file"]["error"])){
if($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else{
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/msword", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.ms-excel", "pdf" => "application/pdf", "txt" => "application/txt");
$filename = $_FILES["file"]["name"];
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];
// siati faaopoopo
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");
// siati fua - 10MB
$maxsize = 200000 * 60;
if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");
// siati MYME
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("general/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
} else{
move_uploaded_file($_FILES["file"]["tmp_name"], "general/" . $_FILES["file"]["name"]);
echo "The file was uploaded successfully.</br></br>";
}
}
else{
echo "Error: There was a problem uploading the file - please try again.";
}
}
} else{
echo "Error: Invalid parameters - something is very very very wrong with this upload.";
}
我一定是漏掉了什么,但还没搞清楚是什么。我试过在 stackexchange 和 google 上查找 application/msword 和其他内容,但一切似乎都是正确的。我得到的错误是:
Error: There was a problem uploading the file - please try again.
请协助
您可能检查了错误的 mimetype,尝试转储文件无法上传的所有 mimetype。
这里是 ms office mimetypes 的一个小概述:office_mime_types
如果你想让你的文件检查得更好更安全一些,你可以这样检查。
$allowed = array(
"xls" => array( "application/vnd.ms-excel" ),
"xlsx" => array(
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
);
// replace this
if ( in_array( $filetype, $allowed ) ) {}
// with this
// this checks also if mimetype of xlsx is just a mimetype of xlsx and nothing else
if ( isset( $allowed[$ext] ) && in_array( $filetype, $allowed[$ext] ) ) {}
我无法将 msoffice 文件上传到服务器上的文件夹,我可以上传 .pdf 和图片文件,但不能上传 .docx、.xlsx 甚至 .txt,从我有限的语法来看,我的所有语法看起来都是正确的知识。这是表格
<form id="Upload" action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="fileSelect">Navigate and choose:</label>
<input type="file" name="file" id="fileSelect"><br><br>
<input class="button" type="submit" name="action" value="Upload to Shared Folder">
</form>
这是 upload_file.php
if(isset($_FILES["file"]["error"])){
if($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else{
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png", "doc" => "application/msword", "docx" => "application/msword", "xls" => "application/vnd.ms-excel", "xlsx" => "application/vnd.ms-excel", "pdf" => "application/pdf", "txt" => "application/txt");
$filename = $_FILES["file"]["name"];
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];
// siati faaopoopo
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed)) die("Error: This file is not an accepted file type.</br></br>");
// siati fua - 10MB
$maxsize = 200000 * 60;
if($filesize > $maxsize) die("Error: File size is larger than the allowed 10MB limit.</br></br>");
// siati MYME
if(in_array($filetype, $allowed)){
// Check whether file exists before uploading it
if(file_exists("general/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. Go back and choose another file or rename the original.</br></br>";
} else{
move_uploaded_file($_FILES["file"]["tmp_name"], "general/" . $_FILES["file"]["name"]);
echo "The file was uploaded successfully.</br></br>";
}
}
else{
echo "Error: There was a problem uploading the file - please try again.";
}
}
} else{
echo "Error: Invalid parameters - something is very very very wrong with this upload.";
}
我一定是漏掉了什么,但还没搞清楚是什么。我试过在 stackexchange 和 google 上查找 application/msword 和其他内容,但一切似乎都是正确的。我得到的错误是:
Error: There was a problem uploading the file - please try again.
请协助
您可能检查了错误的 mimetype,尝试转储文件无法上传的所有 mimetype。
这里是 ms office mimetypes 的一个小概述:office_mime_types
如果你想让你的文件检查得更好更安全一些,你可以这样检查。
$allowed = array(
"xls" => array( "application/vnd.ms-excel" ),
"xlsx" => array(
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
);
// replace this
if ( in_array( $filetype, $allowed ) ) {}
// with this
// this checks also if mimetype of xlsx is just a mimetype of xlsx and nothing else
if ( isset( $allowed[$ext] ) && in_array( $filetype, $allowed[$ext] ) ) {}