What are some best practices for restricting file types that can be uploaded in PHP?
When allowing file uploads in PHP, it is important to restrict the types of files that can be uploaded to prevent security vulnerabilities such as executing malicious scripts. One way to do this is by checking the file extension before allowing the upload to proceed. This can be done by using the `pathinfo()` function to get the file extension and comparing it against a list of allowed file types.
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
$uploadedFile = $_FILES['file']['name'];
$extension = pathinfo($uploadedFile, PATHINFO_EXTENSION);
if (!in_array($extension, $allowedExtensions)) {
die('Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.');
}
// Proceed with file upload code