PHP上传表单、PDF、Doc&;Docx
我正在努力使这个上传代码适用于docx文件,它适用于doc和pdfPHP上传表单、PDF、Doc&;Docx,php,html,upload,Php,Html,Upload,我正在努力使这个上传代码适用于docx文件,它适用于doc和pdf $allowedExts = array("pdf", "doc", "docx"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword")) &&
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
$allowedExts=array(“pdf”、“doc”、“docx”);
$extension=end(分解(“.”,$_文件[“文件”][“名称]);
如果(($_文件[“文件”][“类型”]=“应用程序/pdf”)
||($_文件[“文件”][“类型”]=“应用程序/msword”))
&&($_文件[“文件”][“大小”]<20000000)
&&in_数组($extension$allowedExts))
{
如果($\u文件[“文件”][“错误”]>0)
{
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”;
}
这是不久前一个项目的一部分,我真的不记得怎么做了
我知道这不是最安全的上传方法,但如果有人能帮上忙,我将不胜感激
我想我需要在这里添加另一行:
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
if(($_文件[“文件”][“类型”]=“应用程序/pdf”)
||($_文件[“文件”][“类型”]=“应用程序/msword”))
&&($_文件[“文件”][“大小”]<20000000)
只是不知道什么…谢谢你的帮助
编辑:
所以我已经到了这个阶段(在评论的帮助下!)
$allowedExts=array(“pdf”、“doc”、“docx”);
$extension=end(分解(“.”,$_文件[“文件”][“名称]);
//如果(($_文件[“文件”][“类型”]=“应用程序/pdf”)
//||($_文件[“文件”][“类型”]=“应用程序/msword”))
如果($_FILES[“file”][“type”]=“application/pdf”)
||($_文件[“文件”][“类型”]=“应用程序/msword”)
||($_FILES[“file”][“type”]=“application/vnd.openxmlformats-officedocument.wordprocessingml.document”))
&&($_文件[“文件”][“大小”]<20000000)
&&in_数组($extension$allowedExts))
{
如果($\u文件[“文件”][“错误”]>0)
{
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”;
}
其他的
{
但现在它出现了:解析错误:语法错误,意外的T_BOOLEAN_和第30行的in/var/sites/s/stanation.com/public_html/forms/process/insert.php检查这个MIME类型
application/vnd.openxmlformats-officedocument.wordprocessingml.document
编辑:
这是代码。你缺少括号
<?php
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && ($_FILES["file"]["size"] < 20000000) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Success";
}
}
0)
{
回显“返回代码:”.$\u文件[“文件”][“错误”]。
”;
}
其他的
{
呼应“成功”;
}
}
以下检查将帮助您上载.docx
文件:
$_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
还有另一种方法来完成这项任务。 只需检查文件类型,然后就可以进一步工作了 下面是检查文件类型的代码
$target_dir = "uploads/";
$filename= $_FILES["fileupload"]["name"]; //gets filename with type
$target_file = $target_dir . basename($filename); //uploads/file.type
echo $target_file;
$extension= pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType = strtolower($extension);
if(strcmp($imageFileType,"docx")==0){
echo "Its word file";
}
是的,我发现了一些细节,我只是不知道如何将其添加到允许的类型中?有什么想法吗?我已经很久没有使用任何php了,所以我非常生疏!@AliClarke尝试在
|下添加($_FILES[“file”][“type”]=“application/vnd.openxmlformats of cedocument.wordprocessingml.document”)
($|FILES[“file”][“type”]==“application/msword”)@ShankarDamodaran看起来我们是同时粘贴的。lol看起来我们越来越近了。现在获取此错误。解析错误:语法错误,意外的T_BOOLEAN_和in/var/sites/s/stanation.com/public_html/forms/process/insert.php在线30@ShankarDamodaran啊,英雄所见略同;-)(+1
)干杯
$target_dir = "uploads/";
$filename= $_FILES["fileupload"]["name"]; //gets filename with type
$target_file = $target_dir . basename($filename); //uploads/file.type
echo $target_file;
$extension= pathinfo($target_file,PATHINFO_EXTENSION);
$imageFileType = strtolower($extension);
if(strcmp($imageFileType,"docx")==0){
echo "Its word file";
}