I have this code to validate part of a file upload:

if (!($fileExt == 'doc' || $fileExt == 'docx' || $fileExt == 'pdf')) {
$error = 'The file does not meet the file type requirments. Only Microsoft Document and PDF files are allowed.';
$etype = '1';
include ('inc/error.php');
}

Is it possible to just use an array with all the extensions and then loop through it in the IF statement? Instead of me manually having to writing out the OR statements, can I just define the array and let it loop through, no matter how many elements there are?

Recommended Answers

All 2 Replies

You don't need a loop.

if (!in_array($fileExt, array('doc', 'docx', 'pdf'))) {
// your stuff

Just what I need, thanks very much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.