Closed Thread Icon

Topic awaiting preservation: Reading Zip Files With PHP Pages that link to <a href="https://ozoneasylum.com/backlink?for=25639" title="Pages that link to Topic awaiting preservation: Reading Zip Files With PHP" rel="nofollow" >Topic awaiting preservation: Reading Zip Files With PHP\

 
Author Thread
Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-28-2005 21:13

I'm so proud of myself. I actually wrote my first PHP script and it works! Clients who've licensed photos from me can go to a simple form, enter their invoice number and their files will be displayed for download. Essentially, it just uses part of the invoice number and looks for file names containing that string, but still -- it works!

What I would like to do now is display a list of the files contained in each zip file it finds, so clients can verify which images they'll be downloading. Unfortunately, I need zziplib and DreamHost doesn't have it installed, neither will they install it. They suggested I compile my own installation of PHP and install it myself.

I couldn't even understand how to install zziplib, how am I supposed to compile my own PHP? Besides, that's a whole can of worms I don't have the time to open.

Can anyone think of another way I might be able to do this? I don't want to create a separate text file with each zip's contents or anything -- I'm trying to eliminate steps in my workflow, which is why I created the download script to begin with.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 04-28-2005 21:43

Wes

Congrats on your first script!! =)

This line of code will get the information you want from the zip file on DH:

code:
$zip_info = exec("unzip -l $zip_file")



Just yell if you need help filtering thruogh what gets returned.

-Butcher-

(Edited by butcher on 04-28-2005 21:44)

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-29-2005 06:48

Ahh, good idea!

Unfortunately ... all I can get out of it is "11456006 4 files". I'm going to continue searching, but if anyone has any ideas, feel free to toss them onto my new monitor.

-----Edit-----

Okay, I found exec("zipinfo -1 file.zip") at http://www.annodex.net/cgi-bin/man/man2html?zipinfo+1 but it will only list one of the four file names.

Still searching ...

-----Edit-----

All right, I'm convinced either of these should work, as they work just fine from a command line. In PHP, however, it's only echoing the last line. I assume this is a result of my not using this properly. Exactly how should I be calling for the output?

-----Edit-----

A-ha!

quote:
exec() executes the given command, however it does not output anything. It simply returns the last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
http://us4.php.net/function.exec



As soon as I get some sleep, I'll follow up on this ...



(Edited by Wes on 04-29-2005 08:01)

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 04-29-2005 18:03

Wes, take a look at this "native" PHP library for manipulating ZIP files: http://www.phpconcept.net/pclzip/index.en.php It doesn't require any non-standard external libraries and is actually more flexible than using shell commands...




(Edited by mr.maX on 04-29-2005 18:06)

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 04-29-2005 18:50

hi mr.maX!

mas
Maniac (V) Mad Librarian

From: the space between us
Insane since: Sep 2002

posted posted 04-29-2005 18:50

hola, its mr.maX! nice to see you around

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-29-2005 22:29

Hey there, Max. Thanks for the tip.

For some reason, though, I can't get pclzip to work at all, even when using examples directly from the user guide.

For example, I upload pclzip.lib.php into the current directory and create a new document containing the example from http://www.phpconcept.net/pclzip/man/en/index.php?methods-listcontent (replacing "$zip = new PclZip("test.zip")" with "$zip = "myfile.zip". All I get is a "Call to a member function on a non-object" error.

I even tried pasting the entire contents of pclzip.lib.php into the test file and it didn't work.

I also tried something simple like "echo listContent("images/1017-srgb.zip")" and got a "Call to undefined function: listcontent()" error.

Something doesn't want me reading my zip files.

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 04-29-2005 22:53

For starters, this:

quote:
replacing "$zip = new PclZip("test.zip")" with "$zip = "myfile.zip


Would never work...
the keyword "new" creates a new instance of the class PclZip this is instasiated with the zip-file you want to process.

You must do $zip = new PclZip("myfile.zip")

Now $zip is an object with methods you can use to work with the zipfile, for example $aContent = $zip->listContent();
which according to the manual will give you an array holding all the properties of the zipfile you started with. Loop that array and print each element and you will get the content of the original zip. Like this example from the manual:
http://www.phpconcept.net/pclzip/man/en/index.php?methods-listcontent

You really should read the manual, and probably read up a bit on OOP in PHP, that will help you a lot in getting this to work.

Also, you need zlib compiled into your php installation, I've worked with this here http://www.ozoneasylum.com/13090 and noticed problems with zlib on my phphdev install on the PC, on my linux host it worked perfectly.

/Dan

{cell 260} {Blog}
-{ ?The Internet treats censorship as a malfunction and routes around it.? (-Barlow, John Perry) }-

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-29-2005 22:56

Okay, so in the meantime, I'm trying ...

code:
echo passthru("zipinfo -1 images/1017-srgb.zip");



... which is returning only the list of files, just as I wanted. It returns them like so:

quote:
2004-01-09-137-3702-P02.jpg
2001-06-22-2480-18-PN-P01.jpg
2004-06-21-176-7682-P02.jpg
2004-08-06-188-8860-P02.jpg



Of course, in the browser it all appears on one line, so I attempted to use str_replace to add <br />'s. I tried replacing both "\n" and "\r" but neither worked. This isn't really important, though, because what I want to do ultimately is create an unordered list or something similar.

How do I create an array from what's returned?

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 04-29-2005 23:01

to create an array from a string, try this http://se.php.net/explode
to replace linefeeds with <br/> try http://se.php.net/nl2br

It's all there already

/Dan

{cell 260} {Blog}
-{ ?The Internet treats censorship as a malfunction and routes around it.? (-Barlow, John Perry) }-

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-29-2005 23:04

Got in ahead of me there, DmS.

I see. I guess I somehow misunderstood "new PclZip("test.zip")" to create a new zip file.

quote:
You really should read the manual ...



I did. That doesn't mean I understood all of it.

quote:
Also, you need zlib compiled into your php installation ...



According to DreamHost, it is.

Anyway, I think pclzip is more than I need for this. If I can just parse what I get from zipinfo, that should be sufficient. I can create arrays manually and can add to them using a foreach on the results string search, but I'm not sure how to do it with results like the above.

Edit: Stop typing faster than me!



(Edited by Wes on 04-29-2005 23:05)

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 04-29-2005 23:13

LOL turing into a chat here

You should note that the use of exec() from PHP is sort of controversial since this allows you to run shell commands on the server with the rights that PHP has.

Should the server be misconfigured this could turn out really bad, infact it's more common not to allow this on a webserver than not.

Just so you know
/Dan

{cell 260} {Blog}
-{ ?The Internet treats censorship as a malfunction and routes around it.? (-Barlow, John Perry) }-

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 04-30-2005 20:06

Woohoo! It works! I guess a little sleep helped. (Cuz that's all I got ... a little sleep.)

So if you'd like to see an example of everything at work, take a look at http://wesleytreat.com/temp/index.php -- try entering any number to get a "not found," then enter 3000 to find a single file, then enter 4000 to find multiple files.

Let me know what you think.

Thanks for the help!



(Edited by Wes on 04-30-2005 20:07)

DmS
Maniac (V) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 05-01-2005 01:00

Looks nice
/D

{cell 260} {Blog}
-{ ?The Internet treats censorship as a malfunction and routes around it.? (-Barlow, John Perry) }-

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-02-2005 01:22

Yep, looks good Wes. Nice Job!

Sorry I didn't get back here in time to help you more. I started testing the line of code I gave you and I have to ask a question in regards to it myself.

If I use this:

code:
$zip_info = exec("unzip -l $zip_file")



As Wes said, all it returns is the last line of the infomation. I couldn't find out how to save the whole return in a variable. I was able to do this:

code:
$zip_info = 'zip.txt';
exec("unzip -l $zip_file > $zip_info")


and have all the information show up correctly in the text file.

So how do you get all the info into a variable?

Sorry for borrowing your thread Wes.

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-02-2005 01:46

It's the difference between the exec and passthru commands.



.:[ Never resist a perfect moment ]:.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-02-2005 02:17

But passthru doesn't allow you to save the return does it? It just dumps everything out to the browser. I was wanting to catch it all in a PHP var.

-Butcher-

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-02-2005 02:35

Well you can with an output buffer, but after taking a look at the doc, exec accepts a second argument which will be an output array of all the returned lines.


quote:
If the output argument is present, then the specified array will be filled with every line of output from the command. Line endings, such as \n, are not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().





.:[ Never resist a perfect moment ]:.

Wes
Paranoid (IV) Mad Scientist

From: Inside THE BOX
Insane since: May 2000

posted posted 05-02-2005 06:22
quote:
Sorry for borrowing your thread Wes.


It's okay, I was done with it.

Now I'm on to building a CPU cabinet/CD holder/scanner stand.

So ... how does one get blood out of wood then? I know how to get it out of flesh, so no help needed there.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-02-2005 13:29

Thanks Bitdamaged

I missed that one in the docs.... my bad.

-Butcher-

(Edited by butcher on 05-02-2005 13:30)

« BackwardsOnwards »

Show Forum Drop Down Menu