Click to See Complete Forum and Search --> : Parse name of file inside ZIP files in a directory
Taurus
03-30-2008, 09:56 AM
I have a lot of .zip files on a webserver, all of them have names prod1, prod2, and so on.. Each zip contains one pdf file.
Problem that I lost data about pdf files which containing in these ZIP files. I need know name of each pdf file inside this zips, then I want to rename zip files according to pdf file names. Can the PHP script parse this zip files in a directory, read PDF file name inside and create a list of pdf files?
like this:
Prod1.zip - File_name.pdf
scragar
03-30-2008, 11:05 AM
try something like:
<?php
$path = "/home/www/somewhere/zipDIR/";
$x = opendir ($path);
while($dir = readdir ($x)){
$nfo = pathinfo($path.$dir);
if($nfo['extension'] == 'zip'){
echo "<p><strong>$x</strong> ><br>".nl2br(shell_exec ('unzip -Z -1 "'.$path.$x.'"')).'</p>';
};
};
closedir ($x);
ofcourse this only works on unix systems with unzip installed.
Taurus
03-30-2008, 11:21 AM
tried, got list with same 'id #2' and without info:
Resource id #2 >
Resource id #2 >
Resource id #2 >
Resource id #2 >
and so on
...
scragar
03-30-2008, 11:29 AM
sorry, my mistake:
<?php
$path = "/PATH/WITH/ENDING/FORWARD/SLASH/";// change
$x = opendir ($path);
while($dir = readdir ($x)){
$nfo = pathinfo($path.$dir);
if($nfo['extension'] == 'zip'){
echo "<p><strong>$dir</strong> ><br>".nl2br(shell_exec ('unzip -Z -1 "'.$path.$dir.'"')).'</p>';
};
};
closedir ($x);
?>
Taurus
03-30-2008, 11:57 AM
it works as expected and list file names :)
thank you.
Taurus
03-30-2008, 02:52 PM
One note: does it possible bulk rename zip files via PHP script? I want to bulk rename some ZIP files. ZIP file names and desired file names will be specified inside php script, like this
prod2.zip -> new_name1.zip
prod5.zip -> new_name2.zip
prod10.zip -> new_name3.zip
scragar
03-30-2008, 03:06 PM
http://php.net/rename
theres a rename function that can do them one at a time, I suppose if you has a list you could rename one to the other according to said list:
$names = Array(
'prod2.zip' => 'new_name1.zip',
'prod5.zip' => 'new_name2.zip',
'prod10.zip' => 'new_name3.zip'
);
$dir = '/path/to/files/unless/path/is/working/dir/';
foreach($names as $old => $new){
if(rename($dir.$old, $dir.$new))
echo "'$old' renamed as '$new'<br>";
};
Taurus
03-30-2008, 05:36 PM
I added <?php and ?> close tags and tested, works OK .
Thank you for the help.