What potential pitfalls should be considered when uploading files in PHP?

One potential pitfall when uploading files in PHP is the risk of allowing malicious files to be uploaded to the server, which can lead to security vulnerabilities. To mitigate this risk, it is important to validate the file type and size before allowing the upload to proceed. Additionally, it is recommended to store uploaded files outside of the web root directory to prevent direct access.

// Validate file type and size before allowing upload
$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$maxFileSize = 10 * 1024 * 1024; // 10MB

if (in_array(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION), $allowedFileTypes) && $_FILES['file']['size'] <= $maxFileSize) {
    // Process file upload
} else {
    echo "Invalid file type or size.";
}
```

```php
// Store uploaded files outside of the web root directory
$uploadDirectory = '/path/to/uploads/';
$uploadPath = $uploadDirectory . basename($_FILES['file']['name']);

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadPath)) {
    echo "File uploaded successfully.";
} else {
    echo "Error uploading file.";
}