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.
PHP has command line functions like system and exec that allow you to call your favorite zip program inside the 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.
"C:/Program Files/7-Zip/7z.exe" a -tzip "C:/files/xml.zip" "/files/*.xml"
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.
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.
You can add more files simply by appending more locations to the end.
... "/files/*.xml" "C:/files/css.css" "C:/cow/milk.php"
This would include css.css and milk.php to your zip.
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
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.
Make a new text file. Paste your command inside. Rename its extension from .txt to .bat. It should change icons magically.
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.
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.
To unzip make a new php file and use again the system or exec command.
<? 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.
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.
Files have been successfully zipped and unzipped. Laziness rules.
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.
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.
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,