Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is archive (zip or rar) using php

Tags:

php

How can i check if a file is archived (zip or rar) without knowing the extension. I need to find it using php.

I cannot use Fileinfo because its not installed and installation of any other packages on the server its out of the question.

UPDATE:

Zip module its not installed and i cannot install extra packages. I don't want to use mime_content_type because its deprecated

Thank you

like image 927
keepwalking Avatar asked Feb 01 '12 15:02

keepwalking


People also ask

How check file is ZIP or not in PHP?

php $fh = @fopen($argv[1], "r"); if (! $fh) { print "ERROR: couldn't open file. \n"; exit(126); } $blob = fgets($fh, 5); fclose($fh); if (strpos($blob, 'Rar') !== false) { print "Looks like a Rar.

What is ZipArchive PHP?

Introduction to PHP ziparchive. ZipArchive method in PHP is used to add the file, new directory, and able to read the zip in PHP. ZipArchive is a class rather than a method; ziparchive contains multiple methods; by the use of it, we can perform various operations on the zip in PHP.

How do I know if something is a Zip file?

Other Methods of IdentificationRight-click in Explorer, go to "View" and select "Details." ZIP files will list "Compressed (zipped) Folder" as the file type.

What is the difference between zip and archive?

As mentioned above, the files archived in a Zip file are usually, but not always, compressed. This saves space and also makes it easier to send them as email attachments. Zip files are also convenient for grouping data files.


2 Answers

Output from od -c:

  0000000    R   a   r   ! 032  \a  \0 317 220   s  \0  \0  \r  \0  \0  \0

  0000000    P   K 003 004  \n  \0  \0  \0  \0  \0  \0  \0   !  \0  \0  \0

You could use something like this:

<?php

$fh = @fopen($argv[1], "r");

if (!$fh) {
  print "ERROR: couldn't open file.\n";
  exit(126);
}

$blob = fgets($fh, 5);

fclose($fh);

if (strpos($blob, 'Rar') !== false) {
  print "Looks like a Rar.\n";
} else
if (strpos($blob, 'PK') !== false) {
  print "Looks like a ZIP.\n";
} else {
  print "I dunno.\n";
  exit(1);
}

?>

And my output:

ghoti@baz:~ 423$ ./filephp.php A2.rar
Looks like a Rar.
ghoti@baz:~ 424$ ./filephp.php OLDIE.zip 
Looks like a ZIP.
ghoti@baz:~ 425$ ./filephp.php 1-11-1.PDF 
I dunno.
ghoti@baz:~ 426$ 
like image 143
ghoti Avatar answered Sep 28 '22 03:09

ghoti


To test whether a file is a zip archive, you can attempt to open it as a zip using open_zip function. For rar, you need to have PECL rar (preferably version at least 2.0.0) installed - see http://php.net/manual/en/book.rar.php for more details. The code could look like this:

if(is_resource($zip = zip_open($filename)))
{
    zip_close($zip);
    //this is a zip archive
}
elseif(($rar = RarArchive::open($filename)) !== FALSE)
{
    $rar->close();
    //this is a rar archive
}
else
{
    //this is not a zip or rar archive
}

You may need to do a bit extra work if the archives are password-protected. Read the corresponding php manual pages for more details.

like image 35
Aleks G Avatar answered Sep 28 '22 05:09

Aleks G