Complete the code to check if the uploaded file is an image.
<?php if(isset($_FILES['file'])) { $check = getimagesize($_FILES['file']['[1]']); if($check !== false) { echo "File is an image."; } else { echo "File is not an image."; } } ?>
The getimagesize() function requires the temporary file path, which is stored in tmp_name.
Complete the code to allow only files with .jpg extension.
<?php $filename = $_FILES['file']['name']; $ext = strtolower(pathinfo($filename, PATHINFO_[1])); if($ext === 'jpg') { echo "File extension allowed."; } else { echo "File extension not allowed."; } ?>
The correct constant for getting the file extension is PATHINFO_EXT.
Fix the error in the code to prevent overwriting files by renaming the uploaded file.
<?php $uploadDir = 'uploads/'; $filename = $_FILES['file']['name']; $newName = $uploadDir . time() . '_' . [1]; move_uploaded_file($_FILES['file']['tmp_name'], $newName); ?>
Use the variable $filename to append the original file name safely.
Fill both blanks to check file size and reject files larger than 2MB.
<?php if($_FILES['file']['[1]'] > [2]) { echo "File is too large."; } else { echo "File size is acceptable."; } ?>
The file size is stored in size and 2MB equals 2,000,000 bytes approximately.
Fill all three blanks to safely move the uploaded file with a unique name and check for errors.
<?php if($_FILES['file']['error'] === [1]) { $uploadDir = 'uploads/'; $uniqueName = uniqid('', true) . '.' . pathinfo($_FILES['file']['name'], PATHINFO_[2]); $destination = $uploadDir . $uniqueName; if(move_uploaded_file($_FILES['file']['[3]'], $destination)) { echo "File uploaded successfully."; } else { echo "Failed to move uploaded file."; } } else { echo "Upload error."; } ?>
Upload error code 0 means no error. Use PATHINFO_EXT to get the extension and 'tmp_name' for the temporary file path.