PHP 上传表单、PDF、Doc 和 Docx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18814559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Upload form, PDF, Doc & Docx
提问by Ali Clarke
I'm struggling to make this upload code work for a docx file, it works okay for doc and pdf..
我正在努力使这个上传代码适用于 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"))
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
this is part of a project from a while ago and i honestly can't remember how to do it..
这是不久前的一个项目的一部分,老实说我不记得怎么做..
I know it's not the most secure upload method, but if someone could help it would be appreciated!
我知道这不是最安全的上传方法,但如果有人可以提供帮助,我们将不胜感激!
I'm thinking i need to add another line here:
我想我需要在这里添加另一行:
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword"))
&& ($_FILES["file"]["size"] < 20000000)
Just not sure what.. Help is appreciated!
只是不确定是什么.. 帮助表示赞赏!
Edit: So i've got to this stage (with the help of comments!)
编辑:所以我已经到了这个阶段(在评论的帮助下!)
$allowedExts = array("pdf", "doc", "docx");
$extension = end(explode(".", $_FILES["file"]["name"]));
//if ((($_FILES["file"]["type"] == "application/pdf")
//|| ($_FILES["file"]["type"] == "application/msword"))
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
{
But now its coming up with: Parse error: syntax error, unexpected T_BOOLEAN_AND in /var/sites/s/stanation.com/public_html/forms/process/insert.php on line 30
但现在它出现了:解析错误:语法错误,第 30 行 /var/sites/s/stanation.com/public_html/forms/process/insert.php 中的意外 T_BOOLEAN_AND
回答by Shankar Damodaran
For docx
check this MIME type
对于docx
检查此MIME类型
application/vnd.openxmlformats-officedocument.wordprocessingml.document
EDIT :
编辑 :
Here's the code . You're missing parenthesis
这是代码。你缺少括号
<?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";
}
}
回答by user3405045
This following check will help you to upload .docx
files:
以下检查将帮助您上传.docx
文件:
$_FILES["txtFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
回答by Adnan Ali
There is another way to do the task. Just check the file type and then you can work further.
还有另一种方法可以完成任务。只需检查文件类型,然后您就可以进一步工作。
Here is code to check the filetype.
这是检查文件类型的代码。
$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";
}