Archive

Archive for September, 2009

How to Count Number of Lines in a File with PHP

September 23rd, 2009 No comments

It is a common task to know the size of your files. If you are programming anything using PHP, you should know that there is no exact function to count the number of strings in a file without causing high server load. That’s why I have written a function that uses Linux system command, that works very fast and doesn’t cause any server load. Here it is:

function strcount($filename)
{
if (file_exists($filename))
{
$count=exec(“wc -l $filename”);
return substr($count, 0, strpos($count, ” “));
}
else return 0;
}

Everything seems to be simple, yet usable. Have fun!

Categories: PHP Solutions Tags:

updatedb: command not found – How to Fix

September 16th, 2009 No comments

This problem appears usually if you have a new server and there are no packages installed yet. If you need to use locate command for any reasons, you should run updatedb periodically in order to ensure you have the most recent cache of your file positions.

if you receive command not found error, it usually means you have no slocate package installed. On most modern linuxes you can install it using your package manager (yum, apt-get, etc). You are also welcome to compile it from source, that can be found at http://slocate.trakker.ca/ . I don’t think you should have problems with this package; you will then be able to run updatedb and locate.

It is not recommended to run updatedb if you have lots of files on your machine. In this case updatedb execution can slow down your server. But if the number of files is not so high, locate is probably the fastest way to find the files you need.

Categories: Linux Tricks Tags: