File upload - server side validation

1.4k views Asked by At

please help me with server side validation of upload files.

I my form have 4 inputs for upload files. I need help with multi condition for this 4 inputs. First examle of validation works but only for 1 file. Second example of validation 4 files dont work.
Please help me.

<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

There are 2 answers

0
Scheda On

You're going to want to use a loop on $_FILES.

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

This solution works for me, please how can I simplify?

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