File uploads
Uploading files is a common functionality to many PHP applications. So common that PHP provides a convenient global $_FILES
variable we can use to access uploaded files, or errors behind the file upload tries.
Let's take a look at the following simple file upload form:
<form method="post" enctype="multipart/form-data"> <input type="file" name="photo" /> <input type="file" name="article" /> <input type="submit" name="submit" value="Upload" /> </form>
In order for PHP to pick up the files, we need to set the form method
 value to post
, and enctype
to multipart/form-data
. Once submitted, PHP will pick it up and fill in the $_FILES
variable appropriately:
array(2) { ["photo"] => array(5) { ["name"] => string(9) "photo.jpg" ["type"] => string(10) "image/jpeg" ["tmp_name"] => string(14) "/tmp/phpGutI91" ["error"] => int(0) ["size"] => int(42497) } ["article"] => array(5) { ["name"] => string...