Tags
PHP
Asked 5 years ago
3 Oct 2018
Views 887
lain

lain posted

How to check file is zip(.zip) in php ?

I am downloading some file from remote server
want to check if file is zip or not in php
so how to check it ?
pratik

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().
Post Answer