php - 使用 php 上传 .docx 文件
问题描述
如何使用 php 脚本上传 .doc 和 .docx?我的脚本在 pdf 和 txt 上运行良好,但对于 doc 和 docx 它没有上传。我只需要脚本来上传 pdf doc docx 和 txt。
下面是我的代码
session_start();
$targetfolder = "testupload/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
$ok=1;
$file_type=$_FILES['file']['type'];
if ($file_type=="application/pdf" || $file_type=="application/msword" || $file_type=="text/plain") {
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
$_SESSION['message'] ="The file ". basename( $_FILES["file"]["name"]). " uploaded successfully.";
header("location:lecsubmit?done") ;
}
else {
$_SESSION['message']= "Sorry, your file was not uploaded.";
header("location:lecsubmit?error") ;
}
}
else {
$_SESSION['message']= "You may only upload PDFs, DOCXs, DOCs or TXT files..";
header("location:lecsubmit?error") ;
}
解决方案
您的if
检查 mime 类型的语句不接受 docx 文件。
有关 MS Office 文件的 mime 类型列表,请参见此处。在您的情况下,您也需要允许application/vnd.openxmlformats-officedocument.wordprocessingml.document
。
我可以建议一个小的重构:
$allowedMimes = [
'application/pdf',
'application/msword',
'text/plain',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
];
if (in_array($_FILES['file']['type'], $allowedMimes)) {
// .. continue to upload
} else {
// show supported file types message
}
推荐阅读
- sqlalchemy - 我无法更新这个 FastApi?
- python - 完成一些 R 特定计算后,如何将向量从 Python 传输到 R(并返回)
- c# - C# .NET Core Web API,抛出 AuthorizationException,客户端得到 500。如何让它返回 401?
- linux - udevadm 和 ATA 寻址
- python - 有没有办法从我使用 Kubernetes Python 客户端创建的服务中获取 cluster_ip?
- java - Kafka Streams KTable 外键连接未按预期工作
- ffmpeg - ffmpeg 如何连接视频的两个部分,一个被裁剪,另一个被翻转
- python - 有没有办法在第一个线程完成它的工作后停止多个线程?
- java - War部署到嵌套目录时如何配置Jetty?
- c# - 使用 C# 验证来自 Slack Events API 的请求