What are the common file types allowed for upload in PHP scripts?
When allowing file uploads in PHP scripts, it is important to restrict the types of files that can be uploaded to prevent security risks such as uploading malicious scripts. Common file types that are generally allowed for upload include images (jpeg, png, gif), documents (pdf, docx, txt), and videos (mp4, mov). You can restrict file types by checking the file extension or MIME type before allowing the upload to proceed.
// Check if the uploaded file type is allowed
$allowed_types = array('image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/msword', 'text/plain', 'video/mp4', 'video/quicktime');
$uploaded_file_type = $_FILES['file']['type'];
if (!in_array($uploaded_file_type, $allowed_types)) {
die('Error: File type not allowed.');
}
// Continue with the file upload process
// Your upload code here...
Keywords
Related Questions
- How can PHP developers ensure proper file permissions and directory structures for successful file manipulation?
- What are some potential pitfalls of using while loops in PHP when querying a database?
- What are some best practices for efficiently comparing dates in PHP to find the next upcoming event?