Php 如何在服务器端检查文件格式 var$imagefile=$(“”) 艾特先生({ 键入:“文件”, 名称:'imageFile', id:'imageFile' });

Php 如何在服务器端检查文件格式 var$imagefile=$(“”) 艾特先生({ 键入:“文件”, 名称:'imageFile', id:'imageFile' });,php,jquery,Php,Jquery,上面是我创建文件输入的代码 下面是检查表行中文件格式是否正确的代码: var $imagefile = $('<input />') .attr({ type: 'file', name: 'imageFile', id: 'imageFile' }); 函数验证(){ var marks=parseInt($(“#总重量”).text(); var_qid=“”; var_msg=“”; var allowedTyp

上面是我创建文件输入的代码

下面是检查表行中文件格式是否正确的代码:

var $imagefile = $('<input />')
    .attr({
        type: 'file',
        name: 'imageFile',
        id: 'imageFile'
    });
函数验证(){
var marks=parseInt($(“#总重量”).text();
var_qid=“”;
var_msg=“”;
var allowedTypes=[“jpg”、“jpeg”、“gif”、“png”];
var path=$(“#imageFile”).val();
var ext=path.substring(path.lastIndexOf('.')+1.toLowerCase();
var alertValidation=“”;
//请注意,这只是它的声明。。。
$(“tr.optionandswer”)。每个(函数(){
_qid=$(“td.qid”,this.text();
_msg=“您在问题编号上有错误:”+\u qid+“\n”;
$(“#imageFile”,this).each(函数(){
如果($.inArray(ext,allowedTypes)<0){
alertValidation+='\n\u2022不支持的文件类型';
}
如果(alertValidation!=“”){
返回false;//停止每个循环
}
});
如果(alertValidation!=“”){
返回false;
}
});
如果(alertValidation!=“”){
警报(_msg+alertValidation);
返回false;
}
返回true;
}

现在,这将检查客户端的文件格式,但如何使用php在服务器端也检查它?

一旦文件上载,您只需查看
$\u文件['imageFile']['type']
即可获得mime类型。

我将使用php的finfo\u文件函数:

php页面中的示例:

function validation() {

    var marks = parseInt($("#total-weight").text());    
    var _qid = "";
    var _msg = "";


    var allowedTypes = ["jpg", "jpeg", "gif", "png"];

    var path = $("#imageFile").val();
    var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();


    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function() {


        _qid = $("td.qid",this).text();
        _msg = "You have errors on Question Number: " + _qid + "\n";


         $("#imageFile",this).each(function() {

        if ($.inArray(ext, allowedTypes) < 0) {
        alertValidation += '\n\u2022 Unsupported file type';
        }

         if (alertValidation != "") {
                return false; //Stop the each loop 
            }

    });

        if(alertValidation != ""){
            return false;
        }
    });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}
试试这段代码

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach (glob("*") as $filename) {
    echo finfo_file($finfo, $filename) . "\n";
}
finfo_close($finfo);

编辑

因为用户也询问了检查服务器文件的方法。他可以用这种方法来检查

<?php
$allowedImageTypes = array("image/pjpeg","image/jpeg","image/jpg","image/png","image/x-png","image/gif");
$file = $_FILES['imageFile'];
$fileType = $file['type'];
if (!in_array($fileType, $allowedImageTypes)) { 
    echo "Unsupported file type";
}
else
{
    // Process the file
}
?>


您所说的“在服务器端检查”是什么意思?你想做什么?我被告知,我不仅可以在客户端检查文件格式,还可以在服务器端检查文件格式,我真的不知道这意味着什么,但他们说,如果你不在服务器端检查文件格式,那么会产生安全问题。我相信我需要使用php编码在服务器端检查它。是否有一种方法可以使用此代码在服务器端检查文件,但不显示文件上的信息或文件是否有效,我想使用警报来说明文件是否有效?我已经设置了警报。我应该把它放在我的代码中的什么地方,它只是一个单独的php标记并把代码放在其中,还是需要把代码放在php标记之间的javascript函数中?你可以把它放在php代码中任何你想放的地方。PHP与javascript没有任何关系,所以不要再问这个问题了。这段代码也会进入javascript标记还是在脚本标记之外?当我将代码放入时,它给了我这个错误:注意:未定义的索引:imageFile in/web/stud/u0867587/Mobile_app/QandATable.php第656行,我还希望它在用户点击提交按钮后检查服务器端的文件,那么,我是否将此代码放在myClickhandler()函数之后,该函数控制提交按钮?编写此脚本是为了验证上载图像。请给我几分钟时间建议您检查服务器中文件类型的方法。@user1182476。我修改了我的答案。请查收。
<?php

$filePath = "image/image.jpg";
list($width, $height, $type, $attr) = getimagesize($filePath);
$fileType = image_type_to_mime_type($type);
$allowedImageTypes = array("image/pjpeg","image/jpeg","image/jpg","image/png","image/x-png","image/gif");
if (!in_array($fileType, $allowedImageTypes)) { 
    echo "Unsupported file type";
}
else
{
    // Process the file
}

?>