Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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 文件上载-服务器端验证_Php_Validation_Server Side - Fatal编程技术网

Php 文件上载-服务器端验证

Php 文件上载-服务器端验证,php,validation,server-side,Php,Validation,Server Side,请帮助我进行上传文件的服务器端验证 我的表格有4个上传文件的输入。我需要这4个输入的多条件帮助。第一个验证示例有效,但仅适用于1个文件。第二个验证示例4文件不起作用 请帮帮我 <input type="file" name="orderfile1" id="orderfile1"> <input type="file" name="orderfile2" id="orderfile2"> <input type="file" name="orderfile3" id

请帮助我进行上传文件的服务器端验证

我的表格有4个上传文件的输入。我需要这4个输入的多条件帮助。第一个验证示例有效,但仅适用于1个文件。第二个验证示例4文件不起作用
请帮帮我

<input type="file" name="orderfile1" id="orderfile1">
<input type="file" name="orderfile2" id="orderfile2">
<input type="file" name="orderfile3" id="orderfile3">
<input type="file" name="orderfile4" id="orderfile4">



//example of file validation WORKS!!!!!
//validate file uploads
if(isset($_FILES['orderfile1'])) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile1']['size'] > $maxsize) {
        $errors[] = "Max file size 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile1"]["name"], ".");

    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Only file extension jpeg, png zip rar";
    }       
}   

//example of 4 files validation DONT WORK!!!!
//validate files uploads
if(isset($_FILES['orderfile1'] || $_FILES['orderfile2'] || $_FILES['orderfile3'] || $_FILES['orderfile4'] )) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile1']['size'] > $maxsize || $_FILES['orderfile2']['size'] > $maxsize || $_FILES['orderfile3']['size'] > $maxsize || $_FILES['orderfile4']['size'] > $maxsize) {
        $errors[] = "Max file size 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile1"]["name"], "." || $_FILES["orderfile2"]["name"], "."  || $_FILES["orderfile4"]["name"], "."  || $_FILES["orderfile4"]["name"], "." );

    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Only file extension jpeg, png zip rar";
    }       
}   

//文件验证工作示例!!!!!
//验证文件上载
如果(isset($\u文件['orderfile1'])){
$maxsize=5242880;
如果($\u文件['orderfile1']['size']>$maxsize){
$errors[]=“最大文件大小5MB”;
}
//检测允许的文件扩展名
$valid_file_extensions=数组(“.jpg”、“.jpeg”、“.png”、“.zip”、“.rar”);
$file_extension=strrchr($_FILES[“orderfile1”][“name”],”);
if(!in_数组($file_扩展名,$valid_文件扩展名)){
$errors[]=“仅限文件扩展名jpeg、png-zip-rar”;
}       
}   
//4个文件验证不起作用的示例!!!!
//验证文件上载
如果(设置($_文件['orderfile1']|$_文件['orderfile2']|$_文件['orderfile3']|$_文件['orderfile4'])){
$maxsize=5242880;
如果($_文件['orderfile1']['size']>$maxsize | |$_文件['orderfile2']['size']>$maxsize | |$(U文件['orderfile3']['size']>$maxsize | |$(U文件['orderfile4']['size']]>$maxsize){
$errors[]=“最大文件大小5MB”;
}
//检测允许的文件扩展名
$valid_file_extensions=数组(“.jpg”、“.jpeg”、“.png”、“.zip”、“.rar”);
$file|u extension=strrchr($|u FILES[“orderfile1”][“name”],“$|$| u FILES[“orderfile2”][“name”],“$|$| u FILES[“orderfile4”][“name”],“$|$| u FILES[“orderfile4”],”);
if(!in_数组($file_扩展名,$valid_文件扩展名)){
$errors[]=“仅限文件扩展名jpeg、png-zip-rar”;
}       
}   

您需要在
$\u文件上使用循环

foreach($_FILES as $file) {
    //do your checking here
}

这个解决方案适合我,请问我如何简化

//validate file uploads
if(isset($_FILES['orderfile1'])) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile1']['size'] > $maxsize) {
        $errors[] = "Příloha je příliš velká - max. velikost souboru je 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile1"]["name"], ".");
    // Check that the uploaded file is actually an image
    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Jsou povoleny pouze sobory typu jpg, png, zip, rar";
    }       
}

//validate file uploads2
if(isset($_FILES['orderfile2'])) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile2']['size'] > $maxsize) {
        $errors[] = "Příloha je příliš velká - max. velikost souboru je 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile2"]["name"], ".");
    // Check that the uploaded file is actually an image
    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Jsou povoleny pouze sobory typu jpg, png, zip, rar";
    }       
}

 //validate file uploads3
if(isset($_FILES['orderfile3'])) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile3']['size'] > $maxsize) {
        $errors[] = "Příloha je příliš velká - max. velikost souboru je 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile3"]["name"], ".");
    // Check that the uploaded file is actually an image
    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Jsou povoleny pouze sobory typu jpg, png, zip, rar";
    }       
}

 //validate file uploads4
if(isset($_FILES['orderfile4'])) {
    $maxsize    =  5242880; 
    if ($_FILES['orderfile4']['size'] > $maxsize) {
        $errors[] = "Příloha je příliš velká - max. velikost souboru je 5MB";
    }
    // Detect allowed file extentions
    $valid_file_extensions = array(".jpg", ".jpeg", ".png", ".zip", ".rar");
    $file_extension = strrchr($_FILES["orderfile4"]["name"], ".");
    // Check that the uploaded file is actually an image
    if (!in_array($file_extension, $valid_file_extensions)) {
        $errors[] = "Jsou povoleny pouze sobory typu jpg, png, zip, rar";
    }       
}
我建议你看看它处理多个文件上传与服务器端脚本,你可以修改。手册甚至告诉你如何