In this post I’ll describe the process of creating a tar.gz archive in linux. It is a common practice to create backups or transfer files between servers using such archives. It is quite easy to create them and I’ll show you how to do it.
First of all you need to create a tarball – an archive that contains all the files, that are not compressed. Tar archive file size is the same as the sum of file sizes of the files that are located in the archive. In order to create it, you should issue the following command:
tar -cf <name of the archive>.tar <files of folders that will be in the archive separated by space>
This command will create a tar file. Next you need to compress it. The fastest way to do it in Linux is to use gzip compression (bzip2 often offers better results, but it takes much time and I won’t describe it here). In order to do it just input the following:
gzip <name of the archive created>.tar
This will compress your tarball and will delete your tar archive. The only thing that will be left is tar.gz archive. This is what you need. You can compare file sizes to find, how much space and bandwidth you have won by using the compressed version of your files.
Today I won’t write many letters, I’ll show a simple code that allows to sort file contents by alphabet.
<?
// Getting file into array
$stroki=file(“unsorted.txt”);
//Sorting it without keys – we don’t need them
sort($stroki);
// Writing sorted strings to a new file
$fs=fopen(“./sorted.txt”, “a”);
foreach ($stroki as $string)
{
fwrite($fs, trim($string).”\r\n”);
}
fclose ($fs);
echo “Completed”;
?>
Let’s a take a common situation – you have a directory with a big number of files and you need to sort these files by size. Here is the code that will help you to do this in PHP. Quite short and fast.
<?php
// Path to the directory with big number of files
$dir = “../files/”;
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
// Opening the directory
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
// Checking for file existence
if ($file!=”.” && $file!=”..” && file_exists($dir.$file))
// Creating an array with file names as keys and sizes as values
$sizes[$file]=filesize($dir.$file);
}
closedir($dh);
}
// Sorting the array
arsort($sizes);
// Eching these values
foreach ($sizes as $chinese=>$people)
{
echo $people.” “.$chinese.”<br>”;
}
}
?>
When you work with multiple threads in PHP, you often need to use temporary files. When you have a few files, everything goes well. But when the number of threads is quite big and you need to store your data in temporary files, you may get stuck at this point. A PHP function will save your time by creating random names with mt_rand, md5 and any other functions and word combination. tempnam allows you to create files in the given directory with unique names. You don’t have to worry that the file may exist – file names are absolutely unique and this is guaranteed by PHP
. Please, note that files created by this function are not automatically deleted and you need to have the code that allows to destroy them.
Please, also note that this function output depends on PHP version you use. Starting from 5.2.4 tempnam will return an absolute path to the file so it is ok to use in file operations, but if you need to have links for html, be careful and check your code.
Hope that usage of this function will save your time.