What is the purpose of restricting file uploads to only PDF files in PHP?
Restricting file uploads to only PDF files in PHP helps prevent security vulnerabilities such as malicious code execution through file uploads. By limiting uploads to PDF files, you can ensure that only safe and trusted files are accepted by your application.
// Check if the uploaded file is a PDF
$allowedExtensions = ['pdf'];
$uploadedFileExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array($uploadedFileExtension, $allowedExtensions)) {
// File extension is not allowed, handle error
echo "Only PDF files are allowed for upload.";
} else {
// Process the uploaded PDF file
// Move the file to the desired location
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
echo "File uploaded successfully.";
}
Keywords
Related Questions
- How does using PHP to manipulate HTML elements like meta tags affect SEO and structured data for search engines?
- Where can one locate and interpret error logs for PHP scripts running on IIS 5.0 to diagnose issues like a white screen or error messages?
- What are some best practices for grouping and counting data in PHP arrays based on specific criteria, such as dates or names?