Programming Programming
Google Teaches MySQL
PHP Jukebox Tutorial
Clear Screen in PHP
Rebuilding Apache Logs
Zipping Files with PHP
Del.icio.us Query String Hack
PHP Blogging Tricks
PHP link validator
Google Video for Programming
Newby Webmaster Tips
Cool Pictures All Categories
Internet Videos [ 576 x RSS ]
Front Page [ 118 x RSS ]
Cool Pictures [ 57 x RSS ]
Cool Sites [ 55 x RSS ]
Video Tutorial [ 19 x RSS ]
My Websites
Best of The Internet
Free Video Tutorials
Best of Youtube
Use Google to Download mp3s
Free Quiz Creator
Online Education
Poetry
Famous Poetry
Printable Sheet Music
JimmyR on Youtube
Free Movies Online

Saturday, 26th of January 2008 (26-01-2008)

Zipping and Unzipping Files with PHP

The Power of Laziness

Recently I've been developing things with quite a bit of automation. PHP generates files amazingly fast, but using ftp_put and FTP'ing files into the server can be incredible slow if you have a massive amount of files. It's much faster to upload a single zipped file. I know you could easily zip the files yourself, but alas, laziness makes me write more code and save those valuable seconds.

Using PHP to (compress) zip a file

Command Line Zipping

PHP has command line functions like system and exec that allow you to call your favorite zip program inside the program.

Free Command Line Zipping Program

I'll use 7-zip as an example of a free zipping program becase:

7-zip comes with documentation. 7-zip.chm if you're using windows. It gives all the details about using command line arguments.



Simple Windows Example
"C:/Program Files/7-Zip/7z.exe" a -tzip "C:/files/xml.zip" "/files/*.xml"


WARNING: Backslash is disabled in my blog editor so all code is incorrectly in forward slash.

This tells 7z.exe to zip all (*) xml files in C:/Files into the file C:/Files/xml.zip. If it doesnt exist, create it.

Adding xml, css, and any include files automatically saves a ton of time, especially if there's many files.

Running The Code

This section is mostly for windows users (if you're on linux you're familiar with the command line). Go to start -> run -> CMD. Test your code. Try zipping a bunch of text files in some directory. You may want to type the command in something like windows notepad to make sure it works.

Adding More Files

You can add more files simply by appending more locations to the end.



Adding to previous example
... "/files/*.xml" "C:/files/css.css"  "C:/cow/milk.php"

This would include css.css and milk.php to your zip.

Shell or Bash Script (Optional)

Why? What!?

Now that you've found a working command line argument that will zip the files you want lets make a shell script. It may sound painfully difficult, but it just involves copy and pasting your command to zip into a text file. Easy huh? We'll be calling the file foo for this example

Bash File to Zip with Linux

Type vi foo. Paste your command. Set the file permissions to an executable. Run the command by typing bash foo. Make sure it works as expected.

Windows File

Make a new text file. Paste your command inside. Rename its extension from .txt to .bat. It should change icons magically.

Uploading Zipped Files

Testing

Make a new php file and add the code:

system("foo.bat");
or
system("bash foo");

Make sure it works properly in zipping. If it doesn't make sure you have the php file in the same location as the shell script, or put the directory in the system command.

Uploading

ftp_put()

We successfully zipped the file. Now just use ftp_connect and ftp_put to upload your zipped file. Are we done? Not quite. I don't want to spend time connecting to SSH to unzip the files each time. That takes time, and I'm lazy.

Unzipping the Files Remotely with PHP

Linux

To unzip make a new php file and use again the system or exec command.



whatevernameasdf.php
<? exec("unzip blah.zip"); ?>


Make sure you set the file permissions of that folder to 777 or the lowest allowable for the script to work. Test it. If worried about security, make an obscure named directory no one would think of and make another bash script right under the exec script to transfer all files to the main directory.

Why set permissions? It works fine on root.

Because, now we can use the fopen function to call the php script with a http request so that the server unzips all the files. This is faster and better than ssh to send the command.

Success

Files have been successfully zipped and unzipped. Laziness rules.

Download mp3s Faster than limewire free!

Checkout my Mp3 and files search that uses google to find music/albums, anime, games and movies with direct downloads. It's very safe and easy to use, just search for a song, then right click save as to download it.

My new Blog

See my new funny pictures blog. I accidently deleted some of the old blogging software which I made, so I just rewrote everything from scratch. This blog won't be updated anymore =/.

Free Video Tutorials

I make video tutorials on a variety of topics on youtube. Please help me out by adding me as a friend if you have a youtube account. It really helps.

Free TV from Youtube!

Random: Cute Pets | Video Tutorial | TV | Funny | Anime

Clicking the links above will give you a random but extremely high rated video. There are over 15,000 awesome videos indexed so click as many times as you'd like.



Save To Del.icio.us, Submit to Digg, Stumble it!


Share This Page


Table of Contents

  1. Zipping and Unzipping Files with PHP
  2. The Power of Laziness

  3. Using PHP to (compress) zip a file
  4. Command Line Zipping
  5. Free Command Line Zipping Program
  6. Running The Code
  7. Adding More Files

  8. Shell or Bash Script (Optional)
  9. Why? What!?
  10. Bash File to Zip with Linux
  11. Windows File

  12. Uploading Zipped Files
  13. Testing

  14. Uploading
  15. ftp_put()

  16. Unzipping the Files Remotely with PHP
  17. Linux
  18. Why set permissions? It works fine on root.
  19. Success

Featured on JimmyR.com

Funny Test Answers
[comic] Bunny Suicides
Baby Can't Stop Laughing
Free Movies Online
Video Game Time Attacks
Cool Magic Videos
Picture Everyday Videos
Funny Cat Videos
Complete List of Videos

Add me On Youtube

Add me

My Video Tutorials

Youtube Video Tutorials

Google