Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check file extension in upload form in PHP [duplicate]

I check the file extension for upload or not uploaded. My example methods worked, but now I need to understand if my methods (using pathinfo) is true. Is there another better and faster way?

$filename = $_FILES['video_file']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION); if ($ext !== 'gif' || $ext !== 'png' || $ext !== 'jpg') {     echo 'error'; } 
like image 661
BBKing Avatar asked May 04 '12 21:05

BBKing


1 Answers

Using if( $ext !== 'gif') might not be efficient. What if you allow like 20 different extensions?

Try:

$allowed = array('gif', 'png', 'jpg'); $filename = $_FILES['video_file']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION); if (!in_array($ext, $allowed)) {     echo 'error'; } 
like image 111
Baba Avatar answered Oct 04 '22 15:10

Baba