What are some best practices for handling different file types, such as images, text, and PDF documents, in PHP download scripts?
When handling different file types in PHP download scripts, it is important to set the appropriate headers to indicate the file type and ensure the file is downloaded correctly. For images, use the "image/jpeg" content type, for text files use "text/plain", and for PDF documents use "application/pdf".
// Example for handling image file download
$file = 'image.jpg';
header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
```
```php
// Example for handling text file download
$file = 'text.txt';
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
```
```php
// Example for handling PDF document download
$file = 'document.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);