Recently I work too much with PHP so naturally this blog will go in this direction. I’m trying to be short and at the point, with small but useful snippets of code. I hope that they will help you in some way.
I can’t write long and meaningless chunks of text and when I read developers books I tend to read only the code. In very rare occasions I read some other explanations - they are good when you talk about methodology or algorithm but examples are best when you talk about basics. Actually, this is just an excuse. The main reasons for more code than text in this blog is that I can’t write text.
Tech Blog
Java, JSP, JSF, Struts, Hibernate and Ajax from an everyday life. Nothing serious just some thoughts.
Sep 9, 2011
How to make tar and tar.gz archives with PHP
This question is somewhat painful for me. Maybe I am too stupid. Anyway, if you want to make tar archive without using exec, for example on Windows machine without tar tool or any other stupid reason, here is how to do it with PHP. Its actually very easy if you know how to do it.
For example lets have a directory called dir and we want to make an archive with its content. The solution for our problem is Phar.
Here is the code that do the job:
If you receive error:
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Unable to add newly converted phar "/www/pro/ourarchive.phar.tar" to the list of phars, a phar with that name already exists'
most likely your tar file already exist.
Learn here how to make Zip archives with PHP
For example lets have a directory called dir and we want to make an archive with its content. The solution for our problem is Phar.
Here is the code that do the job:
<?php // we want to archive all files from this directory $dir = "dir/"; // the name of our new tar $phar = new Phar("ourarchive.phar.tar"); // build from our dir $phar->buildFromDirectory($dir); // we are using gzip comppression $phar->compress(Phar::GZ); ?>Look carefully at the name of our archive - the parameter that we give when we create Phar object. It ends with phar.tar. If you use only .tar it will return error:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Cannot create phar 'ourarchive.tar', file extension (or combination) not recognised
This is not fun if you are trying to create tar and you receive this message. So using file name that ends with phar.tar will fix the problem.If you receive error:
Fatal error: Uncaught exception 'BadMethodCallException' with message 'Unable to add newly converted phar "/www/pro/ourarchive.phar.tar" to the list of phars, a phar with that name already exists'
most likely your tar file already exist.
Learn here how to make Zip archives with PHP
How to make Zip archives with PHP - part 1
Actually working with zip archives isn’t that obvious that it looks from the documentation. But if you want an adventure try to make tar or tar.gz. Actually in one of my next posts I will describe the procedure. Now, we are talking about zip here.
Example: Create archive and add file to it
Example: Create archive and add file to it
<?php // the file that we want to compress $file = "path/filetocompress.txt"; // how we want to name the file in the archive $name = "filename.txt"; // we need to create new object $zip = new ZipArchive(); // we are using ZipArchive::open with flag CREATE to make new zip archive if ($zip->open("ournew.zip", ZipArchive::CREATE) === TRUE) { // here the first parameter is the file on our system // the second is the name to be used for this file in our new zip $zip->addFile($file, $name); // close is used for both close and save $zip->close(); } else { echo 'Error'; } ?>
Subscribe to:
Posts (Atom)
Popular Posts
-
I’ll continue my article about JQuery Effects - with this one. It would be short information on how to make custom JQuery slideshow animati...
-
This tutorial tries to provide fast and easy instructions on getting started with Hibernate. You can download Hibernate from http://www.hibe...
-
I receive this nasty error yesterday and it took me some time to figure out the problem. This line passes without any problems $client = n...
-
JMesa is a very useful open source project.It is a dynamic HTML table that allows you to edit, filter, sort, paginate and export your data....
-
This would be simple and easy tutorial on how to use Apache Struts in Java web projects. What do you need for this tutorial? Java Tomcat...
-
Here is one fast and simple example on how to do FTP file uploads with PHP. < ? php $ ftp_server = "ftp.server.url" ; $ ...
-
This question is somewhat painful for me. Maybe I am too stupid. Anyway, if you want to make tar archive without using exec, for example on ...
-
JQuery is a lightweight JavaScript framework that got a lot of attention lately so I decided to write few rows about it. This post-tutorial...
-
You can use JQuery Effects in many different ways. I'll write few examples just to show how cool and fun is to work with this API. Usi...
-
Fast and easy Hibernate example-tutorial (Part 1) In the first part of this tutorial we met the basic functionality of Hibernate. We store a...