User input in php command line

Ever wondered how to capture the user input when writing php command line scripts ?

<?php
 
function getInput($msg){
  
fwrite(STDOUT"$msg: ");
  
$varin trim(fgets(STDIN));
  return 
$varin;
}
 
?>

The function above is being used by me in certain command line scripts, where I need user responses.

Rent A Coder – Resume Page Ratings to RSS

Myself is more or less active at RAC, and wanted to show off my buyers comments in a page for promoting me. And wrote this script rac2rss. Download from here.

The main script is rac2rss.php, change the line [php] $coder_id = “1242159”; [/php], in rac2rss.php to get your resume. [php] $myFileMgr = new fileMgr(‘./cache’,7 * 24 * 60 * 60); [/php] defines the cache, and the cache folder should be world / webserver writable, and it can be outside web path for security reasons, only that the absolute or relative path should be provided, as well the lifetime is in seconds, actually in the said script I have put it as a week.

As a last note, thanks to all members and maintainers of RAC, for making it a wonderful place for all of us.

Moving contents from one folder to another

We always try to store images in a web path where heavy image linking is needed, and mostly the images will be categorised in some sort of directory tree.. to limit the no of files in a folder.

Once editing, for example, re assinging a set of students from one scientist to another, was needed, and the hell, we had to face.. finally the following piece of code was written, for a future reuse.

<?php
function mkdir_r($dirName$rights=0777){
        
$dirs explode('/'$dirName);
        
$dir='';
        foreach (
$dirs as $part) {
        
$dir.=$part.'/';
        if (!
is_dir($dir) && strlen($dir)>0)
                
mkdir($dir$rights);
        }
}
 
function 
xMove($source$target$drop true){
        if(!
is_dir($source)) return false;
        if(!
is_dir($target)) mkdir_r($target);
 
        
$d dir($source);
        while (
false !== ($entry $d->read())) {
                if(!
is_dir($source"/$entry")){
                        
copy($source "/$entry"$target "/$entry");
                        
unlink($source "/$entry");
                }
        }
        
$d->close();
        if(
$drop)
                
rmdir($source);
}
 
?>

If any body is wondering (just a thought) how I have managed the colour highlighting, then better take a look at Phpizer Plugin

php wrapper for zapatec tree widget

To quote the zapatec site ” The Zapatec DHTML Tree is an easy, attractive, and versatile way to display information. You can use the DHTML tree as a menu, a site map, or a way to display your data. Our DHTML Tree works in many different browsers, but if visitors use older browsers, they will still be able to see and use the underlying HTML code. ”

I did find this very good, and in the sense that it provides a method for the Search Engines to see all the links. Recently in an administrative backend, I needed to provide a method to browse multilevel deep category subcategory, where they add new products, to their existing line, for which I did choose the lite version of zapatec suite.

The generation of nested ul/li tags for the tree was a real pain for me, from the existing database. And I thought about a wrapper which could do the job if provided with an array of data rows. Hence I made the class-tree.php. The included file is having an implementation sample which will need the lite version from zapatec site.

The sample implementation with the help of zapatec could generate a tree as shown from the code attached below.

tree screen shot

from

The Data

To start with the implementation, we create an instance of the zptree after including the class-tree.php, and passing the path of zapatec js files.

create instance

Then initiate the tree data using $catTree->treeData = array(); Then go along selecting the data from mysql using the select statement select category_id as id,category_name,parent_category_id from category order by parent_category_id, inside a while loop, the tree data is added to the member array, while($rd = $db->fetch_array($rs)) $catTree->treeData[] = $rd;

The attached class-tree.php also has an example implementation at the end, which should be removed before using in production.

Please excuse me about the sloppy documentation, since I am no good at explaining things.

class-tree.php

File Caching Class

Howdy,

This comes useful when you think about getting content from other sites like RSS Feeds, text feeds, currency conversion rates etc. I tried to make this several times without such an enhancement. Finally the need arose and I have made this a reality.

I will need to find a better code highlighter plugin for wordpress before I can post many php codes and classes. Meanwhile this is being trying for a change:

Continue reading “File Caching Class”

A file proxy class

In a recent web project of mine, it was needed to offload some mp3 files to another server as per the hosting providers specifications. 😉 these could not be overidden since the service was free for a specific purpose. The database was on a different server, and as most of you know, this does not affect php a bit.

But the media bifurcation did take me for a spell. On my test bed, I was using readfile() to read the contents of the mp3file to the browser, after providing correct header tags. In the test server this was working fine, since the file urls were relative ofcourse. I checked through the hosting system using phpinfo() and did confirm the url_fopen wrappers were enabled. But to my dismay, when loaded on to the hosting space, it seemed that the readfile was failing and hence I needed a different method.

Then like a thunderbolt this idea of a file proxy class came to my mind. And this was implemented. It works for me and my project. There may be different view points, as well as enhancements. I would appreciate it if some one could enhance it in case the url_fopen wrappers is disabled in the php configuration.

Continue reading "A file proxy class"