1 Replies - 298 Views - Last Post: 24 October 2019 - 01:13 PM Rate Topic: -----

#1 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

How to check the condition if unspecified file extensions are uploaded

Posted 24 October 2019 - 11:52 AM

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);
            }


This is another approach I tried that doesn't work.

$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
$fileTypeString = implode( ", ", $uploadedFileTypes);
$imageTypes = ["pdf","jpg", "jpeg", "png", "gif"];
$nonImgFiles = false;
$imgFiles = false;

if (!in_array($uploadedFileTypes, $imageTypes)) {
    $nonImgFiles = true;
}

if (in_array($uploadedFileTypes, $imageTypes)) {
    $imgFiles = true;
}

foreach ($_FILES as $file) {
    foreach($file['name'] as $key => $value) {
        if (!empty($value)) { //empty string
            if ($file['error'][$key] != 4) {        
                if ($nonImgFiles == true) {
                    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 {
                    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to check the condition if unspecified file extensions are uploaded

#2 pfar54   User is offline

  • D.I.C Addict

Reputation: 1
  • View blog
  • Posts: 615
  • Joined: 30-April 15

Re: How to check the condition if unspecified file extensions are uploaded

Posted 24 October 2019 - 01:13 PM

This can be deleted or just used as a reference for others. I did the following:

$uploadedFileTypes = $fu->getImageFileTypes();
$extensions = ['pdf','jpg', 'jpeg', 'png', 'gif'];

$differentExtensions = array_diff($uploadedFileTypes, $extensions);
if (count($differentExtensions) > 0) {
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1