Can anyone tell me what's wrong with this variable?

Joined
Jun 16, 2024
Messages
2
Reaction score
0
So i made a variable called $extension and when i use it in my code i keep getting this error: Undefined variable $extension and error 500.
heres the code:

PHP:
$extension = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
$errors['image_file'] .= in_array($extension, $file_extensions) ? '' : 'Wrong file extention ';
 
Joined
Jul 4, 2023
Messages
406
Reaction score
47
AFAIK, when $_FILES['image']['name'] not contain file name with extension (reason, e.g.: not fully uploaded file, etc.) then the function pathinfo return NULL.

Try use $_FILES['image']['type'] is more secure (browser check the type of file and set it up).
PHP:
$errors['image_file'] = '';

$allowed_file_exts = ['image/jpeg', 'image/gif', 'image/png'];
$errors['image_file'] .= in_array($_FILES['image']['type'] ?? '', $allowed_file_exts) ? '' : 'Wrong file extention;';


[ null coalescing operator ?? in php ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,727
Members
45,664
Latest member
Phil79581

Latest Threads

Top