在php中上传docx,zip,rar,pdf,ppt

时间:2015-07-13 07:22:47

标签: php input types

我正在尝试制作文件上传页面,其中docx,zip,rar,pdf,ppt这些扩展只允许上传到这里我写了关于它的代码,但它显示无效的文件类型错误 帮助解决它

$allowed_types = array('zip','rar','docx','ppt');
$tmp = explode(".", $_FILES['file']['name']);
$ext = end($tmp);
$upload_dir ="uploads/assignments/";
if(isset($_FILES['file']) && $_FILES['file']['name'] !=""){
if( ($_FILES['file']['type']=="application/zip")
|| ($_FILES['file']['type']=="application/rar")
|| ($_FILES['file']['type']=="'application/docx'")
|| ($_FILES['file']['type']=="application/ppt")
&& ($_FILES['file']['size']<400000)
&& in_array($ext,$allowed_types))

3 个答案:

答案 0 :(得分:1)

试试这段代码:

 $allowedExts = array('zip','rar','docx','ppt');
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $upload_dir ="uploads/assignments/";
    if(isset($_FILES['file']) && $_FILES['file']['name'] !=""){
    if ((($_FILES["file"]["type"] == "application/zip")
      || ($_FILES["file"]["type"] == "application/rar")
      || ($_FILES["file"]["type"] == "application/docx")
      || ($_FILES["file"]["type"] == "application/ppt"))
      && ($_FILES["file"]["size"] < 400000)
      && in_array ($extension, $allowedExts)) 

答案 1 :(得分:1)

使用此

$allowed =  array('zip','rar','docx','ppt');//allowed types
$filename = $_FILES['file']['name'];//file name

$ext = pathinfo($filename, PATHINFO_EXTENSION);//extension checking

if(!in_array($ext,$allowed) )
{
    echo 'Not Valid';
}
else
{
    echo ' Valid'
}

答案 2 :(得分:0)

使用:

.xls
application/vnd.ms-excel

.xlsx
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.ppt
application/vnd.ms-powerpoint
.pptx
application/vnd.openxmlformats-officedocument.presentationml.presentation

.docx
application/vnd.openxmlformats-officedocument.wordprocessingml.document

.pdf 
application/pdf

For Zip and RAR
'zip'  =>  array('application/x-zip', 'application/zip', 'application/x-zip-compressed','application/force-download','application/octet-stream')


'rar'  =>  array('application/x-rar', 'application/rar','application/x-rar-compressed','application/force-download','application/octet-stream')