Jump to content


Photo

Counting the number of files in a Zip file.


  • Please log in to reply
4 replies to this topic

#1 MilesWilson

MilesWilson
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 14 July 2006 - 04:41 PM

Hello!

Can anyone help me with this? I want to be able to count the number of files in an uploaded zip file.

I have this code:
<?php
$zip = zip_open("images.zip");
/* At this point I would like to echo another peice of javascript, telling the parent how many files there are in the zip */
if ($zip) {
   while ($zip_entry = zip_read($zip)) {
       if (zip_entry_open($zip, $zip_entry, "r")) {
                $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
           	$name = zip_entry_name($zip_entry);
 		$stream = fopen('images/'.$name, "w");
           	fwrite($stream, $buf) or die("helllp!");
          	 zip_entry_close($zip_entry);
		echo "<script language='javascript'> window.parent.displayImage('".$name."'); </script>";
		ob_flush();
		flush();
		sleep(3);
       }
   }
   zip_close($zip);
}

?>

The sleep(3) is there to replace the code I will write shortly, which resizes the image. In the meantime, sleep() means I get a simulation of the time passing.

Thanks in advance.

Miles

#2 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:32 PM

You could do this another way, if the script is running on linux

$command = "tar -tvf $file > file.output"
system ($command, $output);

$lines = file(file.output);
$num_lines = count ($lines);

echo "The Zip file has $num_lines lines";

easy :)

#3 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 14 July 2006 - 05:57 PM

This works only on tar files, If you want it for a zip file, just use a command to list the files in the zip file in the $command statement instead!


#4 MilesWilson

MilesWilson
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 July 2006 - 07:09 AM

Cheers.  Didn't think of running the systems unzipping functions.

Thanks

M

#5 agaul

agaul
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 December 2006 - 06:24 PM

This works only on tar files, If you want it for a zip file, just use a command to list the files in the zip file in the $command statement instead!


and what exactly would that command be? I'm not fluent with Linux at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users