php文件上传,如何限制文件上传类型
我需要检查以下代码(上传的简历和推荐信)是否符合所需类型(pdf或doc或docx)和大小(小于400 kb)php文件上传,如何限制文件上传类型,php,mysql,file-upload,file-type,Php,Mysql,File Upload,File Type,我需要检查以下代码(上传的简历和推荐信)是否符合所需类型(pdf或doc或docx)和大小(小于400 kb) //检查文件扩展名和大小 $resume=($_文件['resume']['name']); $reference=($_文件['reference']['name']); $ext=strrchr($resume,“.”); $ext1=strrchr($reference,“.”); 如果(!($_文件[“恢复”][“类型”]=“应用程序/文档”) ||($_文件[“恢复”][“类
//检查文件扩展名和大小
$resume=($_文件['resume']['name']);
$reference=($_文件['reference']['name']);
$ext=strrchr($resume,“.”);
$ext1=strrchr($reference,“.”);
如果(!($_文件[“恢复”][“类型”]=“应用程序/文档”)
||($_文件[“恢复”][“类型”]=“应用程序/docx”)
||($_文件[“简历”][“类型”]=“应用程序/pdf”))
&&($_文件[“引用”][“类型”]=“应用程序/文档”)
||($_文件[“引用”][“类型”]=“应用程序/docx”)
||($_文件[“参考”][“类型”]=“应用程序/pdf”))
&&($ext==“.pdf”)| |($ext==“.doc”)| |($ext==“.docx”))
&&($ext1==“.pdf”)| |($ext1==“.doc”)| |($ext1==“.docx”))
&&($_文件[“resume”][“size”]<400000)//最多可接受500 kb
&&($_文件[“引用”][“大小”]<400000)){
停止用户}否则{允许上载文件}
这是不符合要求的,允许即使是txt文件通过+的大小限制也没有被检查,有什么问题吗
谢谢,docx的Mime类型是
应用程序/vnd.openxmlformatsofficedocument.wordprocessingml.documentdocx
的Mime类型是应用程序/vnd.openxmlformatsofficedocument.wordprocessingml.document这里是我过去写的一些代码
function checkFileExtension($ext)
{
if ($ext == 'ai' || $ext == 'pdf' || $ext == 'jpg' || $ext == 'jpeg' || $ext ==
'gif' || $ext == 'eps' || $ext == 'tif' || $ext == 'png' || $ext == 'xls' || $ext ==
'xlsx' || $ext == 'doc' || $ext == 'docx' || $ext == 'ppt' || $ext == 'pptx' ||
$ext == 'zip' || $ext == 'rar' || $ext == 'sitx' || $ext == 'psd' || $ext ==
'indd' || $ext == 'dng') {
$pass = (int)1;
} else {
$pass = (int)0;
}
return (int)$pass;
}
$ext = substr(strrchr($_FILES['file']['name'], "."), 1);
$fileAccepted = checkFileExtension($ext);
$fileSize = $_FILES['file']['size'];
if($fileAccepted==1 && $fileSize > '82428800'){
// do stuff
}
这是我过去写的一些代码
function checkFileExtension($ext)
{
if ($ext == 'ai' || $ext == 'pdf' || $ext == 'jpg' || $ext == 'jpeg' || $ext ==
'gif' || $ext == 'eps' || $ext == 'tif' || $ext == 'png' || $ext == 'xls' || $ext ==
'xlsx' || $ext == 'doc' || $ext == 'docx' || $ext == 'ppt' || $ext == 'pptx' ||
$ext == 'zip' || $ext == 'rar' || $ext == 'sitx' || $ext == 'psd' || $ext ==
'indd' || $ext == 'dng') {
$pass = (int)1;
} else {
$pass = (int)0;
}
return (int)$pass;
}
$ext = substr(strrchr($_FILES['file']['name'], "."), 1);
$fileAccepted = checkFileExtension($ext);
$fileSize = $_FILES['file']['size'];
if($fileAccepted==1 && $fileSize > '82428800'){
// do stuff
}
要做到这一点,我通常会使用这样的方法:
$filename = $_FILES['field_name']['name']; // Get the name of the file (including file extension).
$ext = strtolower(substr($filename, strpos($filename,'.'), strlen($filename)-1)); //get the extention in lower case
然后检查文件扩展名是否被接受
还请注意,用户可以简单地更改危险文件的扩展名,因此更安全的做法是使用mime类型进行检查,我通常使用类似的方式:
$filename = $_FILES['field_name']['name']; // Get the name of the file (including file extension).
$ext = strtolower(substr($filename, strpos($filename,'.'), strlen($filename)-1)); //get the extention in lower case
然后检查文件扩展名是否被接受
还要注意,用户可以简单地更改危险文件的扩展名,因此使用mime类型检查更安全,这可能很有用:
首先检查所需的mime类型以验证:
及
然后试着让你的代码更简单
$mimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.presentationml.presentation');
if (in_array($_FILES["resume"]["type"], $mimeTypes))
{
// File's OK
}
else
{
// Bad file !
}
重要提示:用户可能会更改文件扩展名,因此请始终检查扩展名的mime类型intead!!=)这可能很有用:
首先检查所需的mime类型以验证:
及
然后试着让你的代码更简单
$mimeTypes = array('application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.presentationml.presentation');
if (in_array($_FILES["resume"]["type"], $mimeTypes))
{
// File's OK
}
else
{
// Bad file !
}
重要提示:用户可能会更改文件扩展名,因此请始终检查扩展名的mime类型intead!!=)下面的示例仅使用mime类型验证文件,然后检查两者的大小。有关大多数mime类型的列表,请参阅或谷歌
function allowed_file(){
//Add the allowed mime-type files to an 'allowed' array
$allowed = array('application/doc', 'application/pdf', 'another/type');
//Check uploaded file type is in the above array (therefore valid)
if(in_array($_FILES['resume']['type'], $allowed) AND in_array($_FILES['reference']['type'], $allowed)){
//If filetypes allowed types are found, continue to check filesize:
if($_FILES["resume"]["size"] < 400000 AND $_FILES["reference"]["size"] < 400000 ){
//if both files are below given size limit, allow upload
//Begin filemove here....
}
}
}
允许的函数\u文件(){
//将允许的mime类型文件添加到“允许”数组中
$allowed=数组('application/doc','application/pdf','other/type');
//检查上传的文件类型是否在上述数组中(因此有效)
if(在数组中($文件['resume']['type'],允许$)和在数组中($文件['reference']['type'],允许$){
//如果找到允许的文件类型,请继续检查文件大小:
如果($_文件[“恢复”][“大小”]<400000和$_文件[“参考”][“大小”]<400000){
//如果两个文件都低于给定的大小限制,则允许上载
//开始在这里移动。。。。
}
}
}
以下仅使用mime类型验证文件,然后检查两者的大小。有关大多数mime类型的列表,请参阅或谷歌
function allowed_file(){
//Add the allowed mime-type files to an 'allowed' array
$allowed = array('application/doc', 'application/pdf', 'another/type');
//Check uploaded file type is in the above array (therefore valid)
if(in_array($_FILES['resume']['type'], $allowed) AND in_array($_FILES['reference']['type'], $allowed)){
//If filetypes allowed types are found, continue to check filesize:
if($_FILES["resume"]["size"] < 400000 AND $_FILES["reference"]["size"] < 400000 ){
//if both files are below given size limit, allow upload
//Begin filemove here....
}
}
}
允许的函数\u文件(){
//将允许的mime类型文件添加到“允许”数组中
$allowed=数组('application/doc','application/pdf','other/type');
//检查上传的文件类型是否在上述数组中(因此有效)
if(在数组中($文件['resume']['type'],允许$)和在数组中($文件['reference']['type'],允许$){
//如果找到允许的文件类型,请继续检查文件大小:
如果($_文件[“恢复”][“大小”]<400000和$_文件[“参考”][“大小”]<400000){
//如果两个文件都低于给定的大小限制,则允许上载
//开始在这里移动。。。。
}
}
}
OMG,你应该重写它,而不是找到错误:-)首先创建一个允许的mime类型和文件结尾列表,然后对照此列表进行检查…OMG,你应该重写它,而不是找到错误:-)开始创建一个允许的mime类型和文件结尾列表,然后对照这个列表…我知道这篇文章是去年写的,但我遇到了同样的问题。我尝试了上面的答案,但是我的文档和pdf测试文件在safari和chrome中没有通过(没有在ie或ff中测试)。这两个文件都在上面脚本中指定的400kb以下。我找到了链接,得到了正确的MIME:application/msword(doc)| application/pdf(pdf)|和text/plain(txt)
。我取出的另一件东西是引用部分。无法让它工作,所以我对它进行了一些修改以适合我:`//将允许的mime类型文件添加到“允许的”数组--endline--$allowed=array('doc'、'docx'、'txt'、'pdf')--endline--//检查上传的文件类型是否在上面的数组中(因此是有效的)--endline--if(在数组中(pathinfo($\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\['type']值包括引号,因此例如它将是“application/pdf”
。需要删除这些引号,以便比较与_数组中的匹配。例如,在_数组中(str_replace('','',$文件['whatever']['type'],$允许))
我意识到这篇文章是去年写的,但我遇到了同样的问题。我尝试了上面的答案,但我的doc和pdf测试文件在safari和chrome中没有通过(没有在ie或ff中测试)。这两个文件都在上面脚本中指定的400kb以下。我找到了链接,得到了正确的模拟码: