/    Sign up×
Community /Pin to ProfileBookmark

Parse name of file inside ZIP files in a directory

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

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@scragarMar 30.2008 — try something like:
[code=php]<?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> &gt;<br>".nl2br(shell_exec ('unzip -Z -1 "'.$path.$x.'"')).'</p>';
};
};
closedir ($x);
[/code]
ofcourse this only works on unix systems with unzip installed.
Copy linkTweet thisAlerts:
@TaurusauthorMar 30.2008 — 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

...
Copy linkTweet thisAlerts:
@scragarMar 30.2008 — sorry, my mistake:
[code=php]<?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> &gt;<br>".nl2br(shell_exec ('unzip -Z -1 "'.$path.$dir.'"')).'</p>';
};
};
closedir ($x);
?>[/code]
Copy linkTweet thisAlerts:
@TaurusauthorMar 30.2008 — it works as expected and list file names ?

thank you.
Copy linkTweet thisAlerts:
@TaurusauthorMar 30.2008 — 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
Copy linkTweet thisAlerts:
@scragarMar 30.2008 — 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:
[code=php]$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>";
};[/code]
Copy linkTweet thisAlerts:
@TaurusauthorMar 30.2008 — I added <?php and ?> close tags and tested, works OK .

Thank you for the help.
×

Success!

Help @Taurus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...