文件上传 - 服务器端验证

时间:2015-06-12 13:46:54

标签: 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="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";
    }       
}   

2 个答案:

答案 0 :(得分:2)

您将要在$_FILES上使用循环。

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

答案 1 :(得分:1)

此解决方案适合我,请问我该如何简化?

//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";
    }       
}