Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在multipler文件上载程序中验证JS中的多图像格式_Php_Javascript_Jquery_Html - Fatal编程技术网

Php 在multipler文件上载程序中验证JS中的多图像格式

Php 在multipler文件上载程序中验证JS中的多图像格式,php,javascript,jquery,html,Php,Javascript,Jquery,Html,我想在submit或onchange期间验证图像格式,这将通过多个文件加载程序进行浏览 但是在Google等之后,我在JS脚本中找不到多个图像浏览验证。。 这里是HTML <input type="file" id='uploadImg' name='uploadImg[]' multiple > <input id="imgaeupload" value="Submit" type="submit" name='uploadImage'/> 文件上载元素的Files属

我想在
submit
onchange
期间验证图像格式,这将通过多个文件加载程序进行浏览

但是在
Google
等之后,我在
JS
脚本中找不到多个图像浏览验证。。 这里是
HTML

<input type="file" id='uploadImg' name='uploadImg[]' multiple >
<input id="imgaeupload" value="Submit" type="submit" name='uploadImage'/>

文件上载元素的Files属性保存选定文件的列表,您可以在列表上迭代并验证每个文件:

 function validate() {
    var uploadImg = document.getElementById('uploadImg');
    //uploadImg.files: FileList
    for (var i = 0; i < uploadImg.files.length; i++) {
       var f = uploadImg.files[i];
       if (!endsWith(f.name, 'jpg') && !endsWith(f.name,'png')) {
           alert(f.name + " is not a valid file!");
           return false;
       } else {
           return true;

       }
    }
}

function endsWith(str, suffix) {
   return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
函数验证(){
var uploadImg=document.getElementById('uploadImg');
//uploadImg.files:文件列表
对于(var i=0;i
文件上载元素的Files属性保存选定文件的列表,您可以在列表上迭代并验证每个文件:

 function validate() {
    var uploadImg = document.getElementById('uploadImg');
    //uploadImg.files: FileList
    for (var i = 0; i < uploadImg.files.length; i++) {
       var f = uploadImg.files[i];
       if (!endsWith(f.name, 'jpg') && !endsWith(f.name,'png')) {
           alert(f.name + " is not a valid file!");
           return false;
       } else {
           return true;

       }
    }
}

function endsWith(str, suffix) {
   return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
函数验证(){
var uploadImg=document.getElementById('uploadImg');
//uploadImg.files:文件列表
对于(var i=0;i
在javascript中使用通用函数进行图像上传验证

  • 在HTML文件中

  • 在Java脚本文件中

    功能图像测试(字段){
    var regex=/(\.jpg\.jpeg\.png\.gif)$/i;//检查文件类型.jpg、.jpeg、.png、.gif等。
    var target=window.location.protocol+“/”+window.location.host+“/”+window.location.pathname;
    //target=http://localhost/KapdaSearch/V1/Development/public/company
    var fileUploadPath=target.replace(/public.*/,“public/storage/images/”);//这里第一个参数是正则表达式,第二个参数是替换字符串
    //文件上载路径=http://localhost/KapdaSearch/V1/Development/public/storage/images/
    //alert(field.value);//由于某些安全原因,例如C:/fakepath/abc.png,它会为您提供虚假的文件路径
    //我们只需要文件名,所以请使用field.files[0].name而不是field.value
    //一次选择多个图像时的图像验证
    
    对于(var i=0;i在javascript中使用公共函数进行图像上传验证

  • 在HTML文件中

  • 在Java脚本文件中

    功能图像测试(字段){
    var regex=/(\.jpg\.jpeg\.png\.gif)$/i;//检查文件类型.jpg、.jpeg、.png、.gif等。
    var target=window.location.protocol+“/”+window.location.host+“/”+window.location.pathname;
    //target=http://localhost/KapdaSearch/V1/Development/public/company
    var fileUploadPath=target.replace(/public.*/,“public/storage/images/”);//这里第一个参数是正则表达式,第二个参数是替换字符串
    //文件上载路径=http://localhost/KapdaSearch/V1/Development/public/storage/images/
    //alert(field.value);//由于某些安全原因,例如C:/fakepath/abc.png,它会为您提供虚假的文件路径
    //我们只需要文件名,所以请使用field.files[0].name而不是field.value
    //一次选择多个图像时的图像验证
    
    对于(VarI=0;是简单但伟大的解决方案。简单但伟大的解决方案。
     function imageTest(field){
        var regex =  /(\.jpg|\.jpeg|\.png|\.gif)$/i; // Check file type .jpg, .jpeg, .png, .gif etc.
    
     var target = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
     // target = http:  //  localhost  /  KapdaSearch/V1/Development/public/company
    
     var fileUploadPath = target.replace(/public.*/, "public/storage/images/"); // Here 1st parameter is regular expression 2nd is replace string with
     // fileUploadPath = http://localhost/KapdaSearch/V1/Development/public/storage/images/
    
    //alert(field.value); // It gives you fake path of file because of some security reason like C:/fakepath/abc.png
    // We need only file name so use field.files[0].name instead of field.value
    
    // Image validation when you select multiple images at once
     for(var i=0; i<field.files.length; i++){
    var fieldName = field.files[i].name; // Guess file name = xyz.png   
    
    var imgPath = fileUploadPath+fieldName; 
    // http://localhost/KapdaSearch/V1/Development/public/storage/images/xvz.png
    // Check file type is correct or not
    if(fieldName.match(regex)){
        // Check file already exist or not
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('HEAD', imgPath, false);
        xmlhttp.send();
        if (xmlhttp.status == "404") {
            field.setCustomValidity('');
        } else {
            field.setCustomValidity('This '+fieldName+' is already exist. Change file name than upload..');
        }
    }
    else{
        field.setCustomValidity('This '+fieldName+' is invalid..');
    }
    } //End for loop
    } // End function