PHP Get File Type, Size, Extension Before Upload

In this tutorial, you will learn how to get the file type, size, and extension before uploading using PHP.

How to Get File Type, Size, and Extension Before Upload in PHP?

Here are some ways to get file type, size, and extension before uploading:

Retrieving File Type

To retrieve the file type, you can use the mime_content_type() function in PHP. This function reads the first few bytes of the file and returns its MIME type.

Example:

$file_type = mime_content_type($_FILES['file']['tmp_name']);

The line of code $file_type = mime_content_type($_FILES['file']['tmp_name']); retrieves the MIME type of the uploaded file. The mime_content_type() function is a built-in PHP function that takes the temporary file path of the uploaded file as an argument and returns the corresponding MIME type of the file.

Retrieving File Size

To retrieve the file size, you can use the filesize() function in PHP. This function returns the size of the file in bytes.

Example:

$file_size = filesize($_FILES['file']['tmp_name']);

In PHP, $file_size = filesize($_FILES['file']['tmp_name']) is a line of code that retrieves the size of a file uploaded via a form.

Retrieving File Extension

To retrieve the file extension, you can use the pathinfo() function in PHP. This function returns an array containing information about the file path, including the extension.

Example:

$file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

Example 1: To get the file type, size, and extension before uploading it using PHP

Putting it all together, here is an example of how to retrieve the file type, size, and extension before uploading it using PHP:

<?php
if($_FILES['file']['name']) {
// Get file type
$file_type = mime_content_type($_FILES['file']['tmp_name']);

// Get file size
$file_size = filesize($_FILES['file']['tmp_name']);

// Get file extension
$file_extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

// Validate file type, size, and extension
if(($file_type == 'image/jpeg' || $file_type == 'image/png') && $file_size < 1000000 && $file_extension == 'jpg') {
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'], '/path/to/uploads/'.$_FILES['file']['name']);
echo 'File uploaded successfully.';
}
else {
echo 'Invalid file.';
}
}
?>

Conclusion

That’s it, you have learned how to get file type, size and extension using php.

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *