jeudi 24 octobre 2019

How to check the condition if unspecified file extensions are being uploaded at the same time as specified files

I am creating an uploading system. The issue I currently coming across is using the correct function to get the correct execution.

The condition in question is this:

if (!in_array($uploadedFileTypes, $imageTypes)) {
    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND); 
} else {
}

What I have done is made a string list containing the extensions of the files being uploaded. There are two important types of files I am looking for:

  1. Normal image file extensions that I specified in $imageTypes.

  2. Any other type of file.

There can be one file uploaded or more than one. What I am wanting to check for is if the "other type of file" is uploaded (no matter if 50 image files vs 1 "other type of file" is uploaded at the same time). Then if there is the "other type of file" the if statement runs, resulting in file_put_contents being executed.

The only time the else statement should be executed is if only image files specified in $imageTypes are uploaded.

Examples:

If should execute:

[.png, .jpg, .pdf, **.cad**, **.stp**]

[.png, **.par**]

[**.par**]

[**.par**, .png, .png, .png, .png, .jpg, .pdf, .jpeg, .jpg, .png]

The else should execute:

[.png, .jpg]

[.png]

[.jpg, .pdf, .png]

How can I restructure my condition to get this to work correctly?

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
$fileTypeString = implode( ", ", $uploadedFileTypes);
$imageTypes = ["pdf","jpg", "jpeg", "png", "gif"];
//file_put_contents('file_type_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r($uploadedFileTypes, true), FILE_APPEND);

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if ($file['error'][$key] != 4) {
                //if ( empty(array_intersect($uploadedFileTypes, $imageTypes)) ) {
                if (!in_array($uploadedFileTypes, $imageTypes)) {
                    file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);  
                } else {
                }
                $out .= '</ul>';
                $template = str_replace("{filename}", $out, $template);
            }

Aucun commentaire:

Enregistrer un commentaire