Recursive directory iteration with PHP Filesystem API
I have a small stuff to do this morning related to php filesystem api, warming memcached for static files and php template to reduce I/O process overhead in front-end application
$dir = '/path/to/some/dir';
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS;
$iterator = new RecursiveDirectoryIterator($dir, $flags);
$result = array();
foreach ($iterator as $file) {
$result[] = $file->getFilename();
}
print_r($result);
The result will be list of files and directories exist in `$dir`, somehow i need a recursive iterator, luckily php has it all, with small pimp I can have it, just before line where `$result` declaration, I add this line
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
bingo! now it running recursively, i need some filtering here since i only need files with `.txt` or `.html` files, currently it returned all files and directories exists in `$dir` so i created a new iterator filtering class as below
class FilterTxtHtml extends FilterIterator {
public function accept() {
$fileinfo = $this->getInnerIterator()->current();
if (preg_match('/\.(txt|html)$/', $fileinfo)) {
return true;
}
return false;
}
}
save it somewhere, include it and put this line before `$result` declaration,
$iterator = new FilterTxtHtml($iterator);
final code as below:
$dir = '/path/to/some/dir';
$flags = FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS;
$iterator = new RecursiveDirectoryIterator($dir, $flags);
$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
$iterator = new FilterTxtHtml($iterator);
$result = array();
foreach ($iterator as $file) {
$result[] = $file->getFilename();
}
print_r($result);
Easy enough.
Twitter API + jQuery JSONP
Here i will show you how easily fetch non authenticated Twitter API methods using ajax json callback functions with jQuery framework, I use user_timeline method as an example but you can use this sampel to fire other twitter api method as long as its needless authorization.
The TwitterAPI Class
TwitterAPI = {
Statuses: {
user_timeline:function(screen_name, count, callback){
jQuery.getJSON("http://twitter.com/statuses/user_timeline/" + screen_name + ".json?count="+count+"&b="+Math.random()+"&callback=?",
callback);
}
}
};
PHP Error Exception Handling
Got a question from a friend today who is actually a great desktop programmer and just learn PHP since he believe that the future is in the web (hope you’re right dude). He asked me about how to handling error in PHP. Well i’ll write my answer here so can share with others and got feedback when I was wrong.
In PHP there are few options that you can use to handle errors & exceptions during script executions and as developer you must considering not the easiet but the most flexible approach which will help you when debugging your appllication, the more detail error message provided the faster you can fix it. Flexible means its easy for you to change the application behaviour when unexpected things happened during execution like: display error, logging error or even automatically notice you via email.
Install, Configure & Running Memcache in Windows as Service
Installing memcache server in windows is a little bit complicated compared to how it can be done in *nix since theres not yet available ready to use package for win32.
Luckily i found the compiled memcached for win32 (exe) which was made by jellycan, within this exe file you can install configure and running memcached as a service in your windows environment, here are how i made it:
*** updated ****
Newer version available at northscale, thanks to Matt Ingenthron for pointing this.
DynamicWP Image Cube WordPress Plugin
DinamicWP Image Cube is a free plugins for wordpress which will turn your selected images into a 3D image cube slideshow. This plugin will randomly show your image with beautiful fadein/fadeout 3D cube effects.
Released under GPL License means you can use it for all your project or at least use it as a foundation for your next projects for free and without any restrictions.




