0
0
PHPprogramming~10 mins

$_FILES for file uploads in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the uploaded file's name.

PHP
<?php
$filename = $_FILES['userfile']['[1]'];
echo $filename;
?>
Drag options to blanks, or click blank then click option'
Aname
Btmp_name
Csize
Dtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tmp_name' instead of 'name' to get the file's original name.
Confusing 'size' or 'type' keys with the file name.
2fill in blank
medium

Complete the code to check if the file upload was successful.

PHP
<?php
if ($_FILES['userfile']['[1]'] === 0) {
    echo 'Upload successful';
} else {
    echo 'Upload failed';
}
?>
Drag options to blanks, or click blank then click option'
Atype
Berror
Ctmp_name
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Checking 'size' or 'tmp_name' instead of 'error' to verify upload success.
Using 'type' key to check for errors.
3fill in blank
hard

Fix the error in the code to move the uploaded file to the 'uploads/' directory.

PHP
<?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!';
}
?>
Drag options to blanks, or click blank then click option'
Asize
Berror
Ctmp_name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'tmp_name' inside basename() instead of 'name'.
Confusing 'error' or 'size' keys for file name.
4fill in blank
hard

Fill both blanks to create an array of file sizes for all uploaded files in a multiple upload form.

PHP
<?php
$fileSizes = [1]['[2]'];
?>
Drag options to blanks, or click blank then click option'
A$_FILES['userfile']['size']
B$_FILES['userfile']
Csize
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'size' to get file sizes.
Using the wrong variable for the files array.
5fill in blank
hard

Fill all three blanks to check if the uploaded file is a JPEG image and its size is less than 2MB.

PHP
<?php
if ($_FILES['userfile']['[1]'] === 'image/jpeg' && $_FILES['userfile']['[2]'] < [3]) {
    echo 'Valid JPEG file under 2MB.';
} else {
    echo 'Invalid file.';
}
?>
Drag options to blanks, or click blank then click option'
Atype
Bsize
C2097152
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'error' instead of 'type' to check MIME type.
Comparing size to 2 instead of 2MB in bytes.