Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a $_FILE is set in php?

Tags:

file

php

I've created a form with 3 <input type="file"/>

I see that I get an array with array(name=>"").

So I check if ($_FILE["myfilename"]["name"]=="") instead.

This works but it seems rather unusual to me.

I was wondering if there was a better way to check if a file input is set or not?

like image 923
Nicolas de Fontenay Avatar asked May 01 '11 16:05

Nicolas de Fontenay


2 Answers

There is: is_uploaded_file(). When dealing with uploaded files, you should always use it (and its cousin move_uploaded_file()) for security reasons.

like image 105
Pekka Avatar answered Sep 29 '22 11:09

Pekka


You can use empty to check if a variable is blank or not but Pekka's solution is best in this way

if (empty($_FILES["myfilename"]["name"]))

If you are checking that if a variable is set you can use isset function

like image 41
Shakti Singh Avatar answered Sep 29 '22 11:09

Shakti Singh