What potential pitfalls should be considered when using PHP for file uploads and image resizing?

One potential pitfall when using PHP for file uploads and image resizing is the risk of security vulnerabilities, such as allowing malicious files to be uploaded or executed on the server. To mitigate this risk, it is important to validate file types, sanitize file names, and store uploaded files in a secure location outside of the web root directory.

// Validate file type before uploading
$allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['file']['type'], $allowed_types)) {
    die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
}

// Sanitize file name to prevent directory traversal attacks
$filename = basename($_FILES['file']['name']);
$upload_path = '/path/to/uploads/' . $filename;

// Move uploaded file to secure location
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path)) {
    echo 'File uploaded successfully.';
} else {
    echo 'Error uploading file.';
}