PHP Already Have My Function Listed Here

October 31, 2006 | PHP

I’ve written a function to fetch a file from remote server and store it into a string here: http://dev.sandalian.com/baca/4/get-content-of-url-into-string-variable.htm. Then today I realized that PHP already have that function! :D

The function is called get_file_contents(). So, my function is officially deprecated now :D

No comment


Rename Files Into All Lowercase

October 30, 2006 | PHP

I wrote this script when my Windows 98 screwed up my filename. It was index.php and changed into Index.php, in a big number of files within a directory :(

// I name it caser.php
$handle = opendir($argv[1]);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
rename("$argv[1]/$file",strtolower("$argv[1]/$file"));
echo "$file renamed
";
}
}
closedir($handle);
?>

Usage:
C:php4> php.exe caser.php c:apachehtdocs [enter]

Have fun!

 

1 comment


Happy Ied Mubarak – Blog Status: Iddle

October 20, 2006 | Technical Stuffs

Today is my last working day of this month. Me and all moslem around the world are gonna celebrate our holly day, Ied al Mubarak, next week. And all office in this country, will stop their public service for 3 days upto one week, even more.

I will visit my parent’s house in a little village. No internet nor computers there, sad but happy to be home. So, see you next November everybody ^_*

No comment


Copy Remote File Into Local Directory

October 19, 2006 | PHP

Here is a little function used to copy file from remote directory (web server) into our local directory.

/*
This function used to copy remote file into local disk
Example: copyFile("http://test-server.com/file/movie.mpg","myfolder/");
*/
function copyFile($url,$dirname){
@$file = fopen ($url, "rb");
if (!$file) {
echo "Failed to copy $url !
";
return false;
}
else {
$filename = basename($url);
$fc = fopen($dirname."$filename", "wb");
while (!feof ($file)) {
$line = fread ($file, 1028);
fwrite($fc,$line);
}
fclose($fc);
echo "File $url saved to PC!
";
return true;
}
}
?>

This is very basic function. Be carefull, it will replace any existed file with the same name.

 

1 comment


Dealing With Google Sitemap

October 19, 2006 | Technical Stuffs

When I use Webmaster Tools from Google, it requires sitemap of my site. There are several formats allowed, easiest one is RSS 2.0 format. I already have it as my blog feeds.

Then I realize that it need to be in the root directory, while my blog have the XML file within “feeds/” directory. Changing my code will be a pain, so I decided to do some trick.

I simply create a PHP file in the root directory which read the RSS file and echo its content.: // sitemap.php
$feeds = "feeds/rss.xml";
$read = fopen($feeds,"r");
$isi = fread($read,filesize($feeds));
echo $isi;
?>

Then I open my Google’s Webmaster Tool page and submit sitemap.php instead of my RSS file. And the best thing is, it works as I expected ^_*

 

No comment


Detecting WAP/WML or Web/HTML Browser

October 18, 2006 | WML-WAP

I use this little script to detect what browser is being used by the visitor. Is it web browser or WAP browser. Yes, I want my domain accessible by two kind of browser and redirect them to the specific page (ie. web or WAP).

So if visitor open my homepage using web browser (Opera, Firefox, Safari, Internet Explorer, Kameleon, Konqueror, Flock, etc), he will be brought to a web page. Otherwise, if he open my homepage using WAP browser (browser on cellular phone), he will be redirected into a specific page designed for tiny screen.

Here is the script, I write as index.php and put it in the root directory of my homepage. It might be public_html, www, htdocs or whatever it named.

if(eregi("text/vnd.wap.wml",$_SERVER['HTTP_ACCEPT'])){
header('Location: ./wap/');
}
else{
header('Location: ./web/');
}
?>

For your information, directory wap/ is place for my WML scripts and web/ is place for my HTML and PHP scripts.

 

1 comment


Paralel Process In PHP

October 17, 2006 | PHP

In my own experience, paralelizing process in PHP is alot efficient. Not saving your time, but it will save memory usage.

I use paralel method when working with several task at once. For example, fetching several RSS feeds. Let’s give it a try.

Sample case, we must fetch data from 5 (five) rss feeds.

// call this run.php
$feeds = array(
"http://feeds.com/feed1.xml",
"http://feeds.com/feed2.xml",
"http://feeds.com/feed3.xml",
"http://feeds.com/feed4.xml",
"http://feeds.com/feed5.xml"
);

foreach($feeds as $feed){
exec("php.exe fetcher.php?url=$feed");
}
?>

Well, that’s jus’t a simple illustration. Not so good, I think :P

I’ve written more complex task using paralel method for my web crawler and it works as beast!

No comment


Random Text For Signature Used in Forum/Bulettin Board

October 17, 2006 | PHP

There are so many signature generator for forum/bulettin board. And surely we can try to create our own, fully personalized as follow:
random quote

I’ve written this script at September 2004, and now time to share the code.

First, create a text file. Name it quote.txt and write down your text/quotes. One sentence per line.

MAGIC QUOTES by Yeni Setiawan
Another day gone forever
No dog can chew my bone
Catch the falling stars and put it in your pocket
Visit http://sandalian.com for more stuffs
Also visit my personal blog at http://the.sandalian.com/
Hi, I'm 24 years old and looking for sweet girl to date out
Quick brown fox jumps over the lazy dog

Now let’s start coding. First, since we’re going to use image for this thing, tell to browser that this is a PNG file. We don’t use JPG here, I hope you know the reason.
header ("content-type: image/png");
?>

Then define backgound color: /*
define background-color with script.php?bg=r:g:b
if no background color, define as white 250,250,250
*/

$col = $_GET[bg];
if(!$col){
$col = "255:255:255";
}
$col=explode(":",$col);
?>

Next step is read your text file, put it in array and fetch a random string //read the quote from quote.txt, results an array
$quote=file("quote.txt");
$quote=str_replace("\n","",$quote);
$quote=str_replace("\r","",$quote);

//randomize the array
srand ((float) microtime() * 10000000);
$rand_keys = array_rand($quote, 2);
$now= $quote[$rand_keys[0]];
?>

Last but not least, let’s draw an image: $width = strlen($now);
$width = ($width*6.5);
$im = @imagecreate ($width, 18) or die ("cannot initialize new gd image stream");
$background_color = imagecolorallocate ($im, $col[0], $col[1], $col[2]);
$text_color = imagecolorallocate ($im, 0, 0, 0);
imagestring ($im, 2, 3, 3, "\"".$now."\"", $text_color);
imagepng ($im);
?>

To use this script, simply point it as an ordinary image file. Using <img src=”path/to/file.php” />. You can change its background color by adding additional parameters. xxx:xxx:xxx which is RGB value.

<img src=”path/to/file.php?bg=255:100:030″ />

Happy coding! ^_*

2 comments


Get Title From HTML File

October 16, 2006 | PHP

This little snippet will show you how to get string contain the title of an HTML file that located between tag.

First, create a string containing the HTML file:

$a = fopen("html_file.html","r");
$string = fread($a,1024);
?>

Why we just read 1024 bytes? Because the <title> tag mostly located at the top part, which is covered by first 1024 byte. Well, actually it’s up to you how many byte you will read. :p

Then simply filter the string using eregi() function. We use eregi() so it will filter both TITLE and title tag (case insensitive).


// get text inbetween tag.
if (eregi("", $string, $out)) {
$outdata = $out[1];
}

// display it
echo $outdata.
?>

Happy coding! ^_*

2 comments


Succesfully Recovered

October 13, 2006 | Blog News

Yeah, finally this site get its functionallity back. Just hope it might help many people who loves computer as I do ^_*

1 comment