0
//array of image file types
    $fileType_array = array(IMAGETYPE_JPEG, IMAGETYPE_PNG);

    //getimagesize to determine the file_type of the image
    $getImage_thumbnail = getimagesize($_FILES[$thumbnail_fieldname]['tmp_name']);
        $getImage_thumbnail_type = $getImage_thumbnail[2];
    $getImage_desktop_1280x800 = getimagesize($_FILES[$desktop_fieldname_1280x800]['tmp_name']);
        $getImage_desktop_1280x800 = $getImage_desktop_1280x800[2];
    $getImage_desktop_1366x768 = getimagesize($_FILES[$desktop_fieldname_1366x768]['tmp_name']);
        $getImage_desktop_1366x768 = $getImage_desktop_1366x768[2];
    $getImage_desktop_1920x1080 = getimagesize($_FILES[$desktop_fieldname_1920x1080]['tmp_name']);
        $getImage_desktop_1920x800 = $getImage_desktop_1920x1080[2];

    if(in_array($getImage_thumbnail_type, $fileType_array, TRUE)){
        echo "<p>Thumbnail is an image.</p>";
        }

Above is my code to check if the files selected to upload are images, what I would like to do is use in_array just once not several if statements/in_array to check if the file type exists in the array. How can that be?

1 Answer 1

1

You will need to check all this values. But the task can be simplified by working with an array.

$fileType_array = array(IMAGETYPE_JPEG, IMAGETYPE_PNG);

$filenames = array($thumbnail_fieldname, $desktop_fieldname_1280x800, ...);
$files = array();
foreach ($filenames as $filename) {
    if (isset($_FILES[$filename]['tmp_name'])) {
        $resource = getimagesize($_FILES[$filename]['tmp_name']);
        $type = $resource[2];
        if (in_array($type, $fileType_array, TRUE)) {
            $files[$filename] = $resource;
        }
    }
}

This way only files with accepted filetypes will be stored in $files and when you want to change which files you are working with, you only need to update $filenames.

Sign up to request clarification or add additional context in comments.

9 Comments

what is the use of $filenames as $filename?
You can look up foreach() in the manual. This will iterate over the array $filenames and save the current value in $filename (usually it is foreach ($array as $key=>$value) but since the key isn't needed here it doesn't have to be declared)
ow ok..and this will work if the filename in $filenames doesn't contain anything right? since only the accepted values are stored?
What do you mean by "doesn't contain anything"? With this array, in the first iteration $filename = $thumbnail_fieldname, in the second iteration $filename = $desktop_fieldname_1280x800, ...
I mean since these are the files selected by the user, how about if the user doesn't select any file for a file..lets say for desktop_1280x800..its empty it will still work right? or is there any other solution for that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.