How to Remove Directories Recursively with PHP

August 09, 2011

At many times we need to empty a directory. Of course PHP has the filesystem functions like unlink() and rmdir() to delete files and directories. At first we may think that simply usermdir() will solve the problem. Unfortunately it’s not. rmdir() only removes empty directory. If the directory is not empty, it will return false.

In order to remove a directory and its contents, we have to remove its contents first, consisting of files and subdirectories. To make things more complicated, the subdirectories also contains files and another subdirectories in it.

It seems like this is a difficult task to solve. But in contrast, this is a very simple recursive function. See the code below.

<?php
/**
 * This function removes a directory and its contents.
 * Use with careful, no undo!
 */
function rmdir_recursive($dir)
{
  $files = scandir($dir);

  foreach ($files as $file) {
    if ($file == '.' OR $file == '..') continue;
    $file = "$dir/$file";
    if (is_dir($file)) {
      rmdir_recursive($file);
      rmdir($file);
    } else {
      unlink($file);
    }
  }

  rmdir($dir);
}

The function determines if argument is a regular file or a directory. If it is a regular file, it will remove the file. If it is a directory, it will remove its contents first by calling the function itself, and then removes the empty directory.

Note that the function above removes a directory. If what you need is remove directory contents, simply comment out this code:

rmdir($dir)

Posted in How-to, Programming | No comments


Getting Suggestions to Speed Up Your Site with Google Page Speed API

July 30, 2011

A few months ago Google released Page Speed Online, a free service that analyze a web page and make suggestions to improve the page’s speed. Almost immediately, Google released Page Speed API as part of the Google APIs. With this API, developers have the ability to integrate performance analysis in their web performance dashboards.

Start by getting an API key from the API console. Invoke the API by make HTTP request to:

https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=<url>&key=<key>

Replace <url> with the URL of the page you want to analyze, and <key> with your API key.

There is a sample code written in javascript from the documentation, but I think invoking the API from command line will be more useful. Displaying the JSON result to a meaningful display will be easier and you can schedule the script to be automatically run using cron.

Here is the basic PHP script for invoking the API and displaying the suggestions.
Continue reading…

Posted in Tools, Web API, Web Performance | No comments


How to Alter Your Production Database while Keep Your Site Running

July 28, 2011

You know how to alter a MySQL table,

ALTER TABLE t ADD COLUMN c VARCHAR(25);

Only a problem, it takes about 15 minutes for table with 20 million of rows and most likely it will lock the table for write operations.

The time and the complexity is unacceptable for busy production site. Not to mention if we need to alter dozens of table with tons of data. There are users performing read/write operations and we can’t kick them off by displaying an “under maintenance” page (well, actually we can, but it is not recommended).

This is where Facebook’s Online Schema Change for MySQL come into resque. It is a simple but complex utility to perform schema changes while keep your site running and no one realize that something had happened to your database.

This tool has several phases:

  • Copy – make a copy of the original table.
  • Build - work on the copy until its ready.
  • Replay - propagate the changes that happened on the original table to the copy table.
  • Cut-over – rename the copy table as the original.

You can read the full documentation at Facebook Notes and the source is here.

Let’s put the script into the test. I’ll use the table from my URL shortening script:
Continue reading…

Posted in Tools | No comments


Node.js: Javascript on the Server Side

July 27, 2011

Today I spent several hours playing with node.js, an amazing technology that lets your javascript code live on the server side.

Node.js is an event-driven I/O server-side Javascript based on Google’s V8 Javascript engine. It is a framework for writing event-driven web servers in Javascript. Using this software, we can develop web apps using HTML5, CSS, and Javascript both for the frontend and the backend.

I have successfully installed node.js on my Ubuntu box using the installation guide. There is also an excellent book about node.js that covers all of the basics.

Here is a simple web server written in Node:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('<h1>Hello World!</h1>');
  res.end();
}).listen(8888);

console.log('Server started.');

Open http://localhost:8888 in your browser, it should display big “Hello, World!” text.

There is a downloadable chat application built on top of node.js. You can start there for learning how to code node.js, and read the documentation.

This piece of software is amazing. But its still very young and I’m not sure that Node is ready for production servers yet. However the creator has done a great job and I’ll keep an eye on this project.

Posted in Presentations, Tools | No comments


PHP Script for Converting Relative to Absolute URL 2

July 26, 2011

This is a very simple PHP script to convert relative URL to its absolute path, given a base URL. e.g: converting lena.jpg to http://example.com/a/b/lena.jpg.

When I searched Google for scripts to convert relative to absolute URL, I came accross a site that has done that, only in Perl. It is a very simple script compared to the others, so I tried to port the code into PHP.

Note what the Perl geek said about this:

“I wrote this mainly because someone asked how to do this in PHP. Turns out there is nothing in like the venerable URI module in all of PHP-dom (how pathetic is that for a language as web-centric as it?)”

Continue reading…

Posted in Programming | 2 comments


Google I/O 2011: Creating Accessible Interactive Web Apps using HTML5

July 25, 2011


View slides

A presentation by Dominic Mazzoni and Rachel Shearer about using HTML5 techniques for making your web apps accessible to all users.

Keypoints:

  • Build web apps that accessible by everyone. Consider users with assistive technology.
  • Use clean HTML and standard tags.
  • Manage focus.
  • Add key handlers – custom controls should respond to keys (Enter, arrows, etc).
  • Add ARIA for screen readers
  • Unplug your mouse to test
  • Chrome extensions for accessibility: ChromeVox, ChromeVis, ChromeShades.

Good presentation, simple techniques, interesting topics. I never thought that my web apps would be used by users with assistive technology. I’ll consider this in my next project.

Posted in Presentations | No comments


How To Create URL Shortening Service using Simple PHP

July 21, 2011

Fellow StackOverflow user Marcel Jackwerth gave a good answer on how to create URL shortener:

You need a Bijective Function f (there must be no x1 != x2, that will make f(x1) = f(x2); and for every y you will find an x so that f(x)=y). This is necessary so that you can find a inverse function g('abc') = 123 for your f(123)='abc' function.

How to convert the id to a shortened URL:

  • Think of an alphabet you want to use. In your case that’s [a-zA-Z0-9]. It contains 62 letters.
  • Take the auto-generated unique numerical key (auto-incremented id): for example 125 (a decimal number)
  • Now you have to convert the 125 (base 10) to X (base 62). This will then be {2}{1} (2×62+1=125).
  • Now map the symbols {2} and {1} to your alphabet. Say {0} = ‘a’, {25} = ‘z’ and so on. We will have {2} = ‘c’ and {1} = ‘b’. So ‘/cb’ will be your shortened URL.

How to resolve a shortened URL abc to the initial id:

  • If you want to do this in reverse, it’s not very diffcult. ‘e9a’ will be resolved to “4th,61st,0th letter in alphabet” = {4}{61}{0}, which is 4×62×62 + 61×62 + 0 = 19158. You will then just have to find your database-record with id 19158.

Some people has also suggested for simply convert the auto-incremented ID using PHP’s base_convert function. It can convert numbers to base 36 and represented the result with [0-9a-z]. Its simple with only a few lines of code. But I want to see how real URL shortening services like bit.ly and goo.gl work. And Marcel’s answer above is a good place to start.

Continue reading…

Posted in Programming, Tools | Comments Off


Re-Invent Twitter and Tuning Its Performance

July 13, 2011

High Performance Web Sites is an interesting and challenging topic . Having read numerous presentations and Steve Souder’s book, I want to see an optimized, high performance real world application.

Twitter is interesting. While I think that it is weird, so complex, and full with unhuman language, Twitter is one of the most successfull web applications on the planet. It took me days and a lot of coffee before I understand how Twitter works. At the end I have to admit that it is cool and extremely simple. It is also a perfect application for doing some performance hacking and so I re-invent Twitter.

Continue reading…

Posted in Projects, Web Performance | No comments


12 PHP HTTP Scripts You Should Know

July 05, 2011

Curl is great and we use it a lot for accessing web services. While we can do almost HTTP stuffs with Curl, I find it very difficult to use. It took me days to find out how to do simple things with Curl before I decided to write my own HTTP client. The library is built from code snippets I got mostly from the PHP Manual, especially from the comments section. Continue reading…

Posted in Programming | No comments


Setting up Linux Server for Web Development

July 01, 2011

Most of us develop web applications on our local PC, test with local XAMPP installation and upload the code to web server when everything is ok. While this is ok, for more advanced web application we need to set up a separate Linux server. We do this for several reasons:

  • We want to mimic production server.
  • Most of us develop web apps in Windows, and installing some useful software like Memcached/APD/Xdebug on Windows is painful.
  • We can setup the environment to test the site’s performance under heavy load.
  • etc.

Continue reading…

Posted in Computer Networking, Programming | No comments