File Uploads
What This Tutorial Covers
Upload Forms
Multipart form data and PHP's $_FILES superglobal.
Validation
Type, size, and security checks before storing.
Storage
Save to filesystem or cloud storage like S3.
Noble Desktop's Full-Stack Web Development Certificate teaches modern back-end development — concepts that carry across PHP, Node.js, and Python.
Discover how to securely and effectively upload files using PHP and MySQL, along with understanding fundamental concerns tied to file uploads such as computer security and legal issues.
In your code editor, open upload.php from the phpclass folder.
Just above the switch expression, add the following bold code:
$file_info = new finfo; switch($_FILES['myFile']['type']) {This initializes the finfo method and saves into a new object called $file_info.
Let’s use the new object. Add the following bold code:
$file_info = new finfo; $mime_type = $file_info->buffer(); switch($_FILES['myFile']['type']) {This sets a new variable called
$mime_type. We also use the$file_infoobject and run the methodbuffer()on it. You can think of an object like a little application that you can run functions on. The -> arrow says to run the functionbuffer().Next we need to specify the parameters for the
buffer()function. We need to send it the actual file as the first parameter, and the second parameter will tell it what info we want to get from the file. In this case, we want to get the mime type. Add the following bold code:$file_info = new finfo; $mime_type = $file_info->buffer(file_get_contents($_FILES['myFile']['tmp_name']), FILEINFO_MIME); switch($_FILES['myFile']['type']) {In order to get the actual file we need to use the
file_get_contents()function. Inside that we send it the temporary file that PHP has created. The second parameter forbuffer()is FILEINFO_MIME which tells PHP that we want the MIME type.Let’s echo
$mime_typeto see what we have so far. Add the following bold code:$file_info = new finfo; $mime_type = $file_info->buffer(file_get_contents($_FILES['myFile']['tmp_name']), FILEINFO_MIME); echo $mime_type; exit;Save the page and then in a browser go to:
- Mac: localhost:8888/phpclass/upload.php
- Windows: localhost/phpclass/upload.php
Click the button to choose a file, then navigate to:
- Mac: Hard Drive > Applications > MAMP > htdocs > phpclass > files
- Windows: C: > xampp > htdocs > phpclass > files
Double–click bird.jpg.
Click Upload. You’ll see:
image/jpeg; charset=binary
If you are on Windows, and you see an error that begins with Fatal error: Class ‘finfo’ not found in… make sure to restart your computer after completing the sidebar earlier in the exercise for activating the finfo function in XAMPP.
Switch back to your code editor.
Delete the echo statement. We’re done with it.
We want to get only the first part of this string. Our switch statement is expecting just the first part, not the charset=binary. One way to get just the first part is to turn the string into an array using the
explode()function. We can turn it into a two-part array with the semi-colon as a delimiter. Add the following bold code:$file_info = new finfo; $mime_type = $file_info->buffer(file_get_contents($_FILES['myFile']['tmp_name']), FILEINFO_MIME); $myMimeArr = explode(';', $mime_type); exit;This makes a new array called
$myMimeArr.Let’s quickly print
$myMimeArrto see what we have. Add the following bold code:$file_info = new finfo; $mime_type = $file_info->buffer(file_get_contents($_FILES['myFile']['tmp_name']), FILEINFO_MIME); $myMimeArr = explode(';', $mime_type); print_r($myMimeArr); exit;Save the page and then in a browser go to:
- Mac: localhost:8888/phpclass/upload.php
- Windows: localhost/phpclass/upload.php
Click the button to choose a file, then navigate to:
- Mac: Hard Drive > Applications > MAMP > htdocs > phpclass > files
- Windows: C: > xampp > htdocs > phpclass > files
Double–click bird.jpg.
Click Upload. You’ll see:
Array ( [0] => image/jpeg [1] => charset=binary )
Perfect. Now we can just use the first element in the array.
Switch back to your code editor.
Delete the
print_rfunction and the exit statement, too.Edit the switch statement as shown in bold:
switch($myMimeArr[0]) {Save the page and then in a browser go to:
- Mac: localhost:8888/phpclass/upload.php
- Windows: localhost/phpclass/upload.php
Click the button to choose a file, then navigate to:
- Mac: Hard Drive > Applications > MAMP > htdocs > phpclass > files
- Windows: C: > xampp > htdocs > phpclass > files
Double–click uploadme.txt.
Click Upload.
You should see an error message: That file is not the correct type.
Hit the back button in the browser and this time choose a JPG, GIF, or PNG from the folder. The file should upload (check for it in the upload_test folder).
Switch back to your code editor, and close any files you may have open.