Usually after completing a long project I find that I’ve created various extraneous files in the project directory; like zip files or maybe a few big graphic files or some huge MySQL dumps. If the directory sizes are small I can manually delete those unwanted files. But if the directory sizes are big or if they are nested deeply, than it can be quite time consuming. Maybe you left a couple of huge MySQL dumps somewhere and forgot to delete them, thus increasing the project file size. And if you are trying to do the cleanup on a online server then it can be even more painful.
Or maybe you are just curious to find how various types of files are taking up your directory space.
Whatever the reason, below is a small php script that displays the distribution of files in a particular directory and its sub-directories by its type. This can be handy if you would like to see which files are taking up space in your project. Although you can easily do such kind of things with a variety of desktop tools, the following code can easily be used online, or integrated into your existing php application.
cURL is a Swiss army knife of web content processing. Programmers use it for a number of things everyday. One interesting feature the cURL library offers that many programmers are unaware of is the parallel execution of requests.
curl has two major modes of request execution: ‘easy’ mode and the ‘multi’ mode. Most people use the ‘easy’ mode – in this mode when we issue multiple requests, the second request will not start until the first one is complete. This is known as synchronous execution, and this is the one we normally use. This means that if we have 100 requests to process, each will be processed in a linear manner, which could take a lot of time. This is where ‘multi’ mode comes to the rescue. In this mode all requests can be handled in parallel or asynchronously. And it can be quite handy and time saving on many occasions. The ‘multi’ or parallel mode is handled by the following curl functions:
curl_multi_init(); curl_multi_add_handle(); curl_multi_select(); curl_multi_exec(); curl_multi_getcontent(); curl_multi_info_read(); curl_multi_remove_handle();
- Command line tools
- Desktop GUI-based applications
PHP supports one error control operator: the at sign (@). When prepended to an expression any error generated by that expression will be ignored. It can also be useful for hiding errors generated by various functions.Take the following simple example:
$var = $_GET['data'];
If the ‘data’ parameter is not defined the expression will generate an error.
Notice: Undefined index: data in /var/www/test.php on line 9
You can hide the error using the silence @-operator.
$var = @ $_GET['data'];
Although quite useful at some times, using the @-operator can have some annoying side effects. Say you are using some external libraries in your application which uses the @-operator. If everything works fine than good. But if the library is generating some errors than it becomes difficult to point the exact location where the error occurs, as the @-operator hides it. If the external library is large, it becomes a headache to remove all the @ from the code. One nice option I found is the Scream Pecl extension. The extension allows you to easily disable the @-operator in your code without making any actual changes to the code.
A couple of days back while writing some date code for a messaging service, I required to print the date of the messages in a relative format – ‘today, ‘yesterday’, 3 weeks ago’ etc. I wrote a small function for the same. A sample run of the function is shown below.
echo DateToWords(time()) . "<br />"; echo DateToWords(time() - (3600 * 24 * 1)) . "<br />"; echo DateToWords(time() - (3600 * 24 * 4)) . "<br />"; echo DateToWords(time() - (3600 * 24 * 7)) . "<br />"; echo DateToWords(time() - (3600 * 24 * 14)) . "<br />"; echo DateToWords(time() - (3600 * 24 * 100)) . "<br />"; echo DateToWords(time() - (3600 * 24 * 366));
And the output for the above. For dates above 1 year it returns the actual date.
today yesterday 4 days ago 1 week ago 2 weeks ago 14 weeks ago 06-17-2009
As a programming language, PHP has many advantages but security has always been a major issue. Partially these security problems are inherent to the language itself because PHP was meant to be an easy and powerful programming language, while security came second. However, when you add bad coding and non-adherence to even the basic security rules, the situation gets out of control.
Fortunately, it is possible to fix PHP vulnerabilities and make PHP applications more secure. Some of the defenses are common for all programming languages, while others are found only in PHP. Here are some of the best defenses you have when you want to fix PHP vulnerabilities and make your site more secure.
Automatically creating fake or sample data is a frequent requirement for front-end web developers. Although usually not tedious, there are times when you need to quickly and automatically generate structured data for your html forms or CMS systems for testing purposes.
Interface design is hard. Which is why most programmers turn a blind eye to it. During collaborative development I frequently encounter fellow programmers remark something to the following effect: ‘…do not worry, the users are not idiots, they will understand for what these buttons have been provided, no need to provide tool-tips or any help, lets get these code working and show it to the client.’
Interface design or rather usability design is usually left as an after thought; a colorful facade that you stick on to your backend code.
The following two videos provide a stark reminder, that for most of the time these are the people we develop software for.
Geographical information integration is rapidly becoming an integral part of many websites. People use geographic data for a wide variety of applications. From location based content targeting, censoring information by geographic areas to analyzing website traffic by region. It is surprising how much free geographic information is available on the web. GeoNames is one such service.
This site is a digital habitat of Sameer, a freelance web developer working in PHP & MySQL.More