简体   繁体   English

php javascript表单上传文件验证<input>

[英]php javascript form validation for upload file <input>

want to get a validation script to validate the file upload box onsubmit. 想要获得一个验证脚本来验证提交时的文件上传框。

check the extension of the file and validate accordingly to the extension of file loaded 检查文件的扩展名并相应地验证加载的文件的扩展名

Check here: http://jsfiddle.net/uh2Gn/ 在这里检查: http : //jsfiddle.net/uh2Gn/

HTML: HTML:

<form method="post" enctype="" onsubmit="return validate()">
    <input type="file" id="file" />
    <input type="submit" />
</form>

JavaScript: JavaScript:

function validate() {
    var filename=document.getElementById('file').value;
    var extension=filename.substr(filename.lastIndexOf('.')+1).toLowerCase();
    //alert(extension);
    if(extension=='jpg' || extension=='gif') {
        return true;
    } else {
        alert('Not Allowed Extension!');
        return false;
    }
}

Keep in mind that this is only for user convenience, that he doesn't go theu a long submit process to get an error at the server, cuz for sure you must implement a check at server-side. 请记住,这仅是为了方便用户,他不会花很长时间来提交服务器错误,请确保您必须在服务器端实施检查。

If you don't mind jquery, then you can use the validation plugin, which is available here . 如果您不介意使用jquery,则可以使用验证插件,该插件在此处可用。 A brief introduction and small demo is available here . 一个简短的介绍和小的演示,请点击这里

I use the following in my php script for validation. 我在我的PHP脚本中使用以下内容进行验证。

$status_file = validate_and_upload("project_file");


function validate_and_upload($input_tag_name)
{
    $allowedExts = array("gif", "jpeg", "jpg", "png", "ppt", "doc", "pdf", "xls", "xlxs", "txt", "docx");
    $filename = $_FILES[$input_tag_name]['name'];
    if ( !$filename )
      return 0;
    $extension = pathinfo($filename, PATHINFO_EXTENSION);

    if ( ($_FILES[$input_tag_name]["size"] < 33554432) && in_array($extension, $allowedExts) ) // 33554432 is 32.00 MB
        {
            if ($_FILES[$input_tag_name]["error"] > 0) 
            {
                echo "Return Code: " . $_FILES[$input_tag_name]["error"] . "<br>";
                return -1;
            } 
            else 
            {
                if (file_exists("/sites/default/files/private/" . $_FILES[$input_tag_name]["name"])) 
                {
                    echo $_FILES[$input_tag_name]["name"] . " already exists. ";
                    return 2;
                } 
                else 
                {
                    $hard_disk_upload_directory = "C://xampp/htdocs/mywebsite/sites/default/files/private/";
                    if (move_uploaded_file($_FILES[$input_tag_name]["tmp_name"], $hard_disk_upload_directory . $_FILES[$input_tag_name]["name"]))
                        return 1;
                    else
                        return -1;
                }
            }
        } 
        else 
        {
            echo "<script>alert('Invalid file'); window.location.href='http://mywebsite/home';</script>";
            return -1;
        } 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM