pratik
answered Apr 25 '23 00:00
To check whether a file is a zip file (.zip) in PHP, you can use the pathinfo () function to retrieve the file extension and then compare it to the string "zip". Here's an example:
/
/ Define the file path
$file_path = '/path/to/your/file.zip';
// Retrieve the file extension
$extension = pathinfo($file_path, PATHINFO_EXTENSION);
// Check if the extension is 'zip'
if ($extension === 'zip') {
echo 'The file is a zip file.';
} else {
echo 'The file is not a zip file.';
}
In this example, the pathinfo () function retrieves the extension of the file located at $ file_path . By specifying the PATHINFO_EXTENSION flag, pathinfo() returns only the extension, without any other file information. The resulting extension is then compared to the string "zip" using an if statement to determine whether the file is a zip file.
It's important to note that comparing file extensions alone is not a foolproof method for determining file types, as file extensions can be easily changed. For more robust file type checking, consider using a library such as finfo or mime_content_type().