Complete the code to access the uploaded file's name.
<?php $filename = $_FILES['userfile']['[1]']; echo $filename; ?>
The name key in $_FILES holds the original name of the uploaded file.
Complete the code to check if the file upload was successful.
<?php if ($_FILES['userfile']['[1]'] === 0) { echo 'Upload successful'; } else { echo 'Upload failed'; } ?>
The error key in $_FILES holds an error code. A value of 0 means no error.
Fix the error in the code to move the uploaded file to the 'uploads/' directory.
<?php $uploadDir = 'uploads/'; $uploadFile = $uploadDir . basename($_FILES['userfile']['[1]']); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) { echo 'File is valid, and was successfully uploaded.'; } else { echo 'Possible file upload attack!'; } ?>
The basename function needs the original file name, which is in the name key.
Fill both blanks to create an array of file sizes for all uploaded files in a multiple upload form.
<?php $fileSizes = [1]['[2]']; ?>
To get all file sizes in a multiple upload, use $_FILES['userfile']['size']. Here, $_FILES['userfile'] is the array, and 'size' is the key.
Fill all three blanks to check if the uploaded file is a JPEG image and its size is less than 2MB.
<?php if ($_FILES['userfile']['[1]'] === 'image/jpeg' && $_FILES['userfile']['[2]'] < [3]) { echo 'Valid JPEG file under 2MB.'; } else { echo 'Invalid file.'; } ?>
The type key holds the MIME type, size holds the file size in bytes, and 2MB equals 2097152 bytes.