scriptygoddess

11 Mar, 2003

PHP at the command prompt

Posted by: dave In: Admin-type scripts

Figured it was about time I actually posted something…

Little known to many people, PHP actually makes an excellent system level scripting language. Let's take a quick look at scrapping Perl & using PHP for a simple problem.

Setup:
I've got a bunch of m3u files that contain reference to single mp3 files on a friend's web server. I want to download all the mp3s, but don't want to open each m3u file & copy and paste the http link. Let's use PHP for the task.

First off, this depends on how you have PHP installed, esp. on Linux/Unix systems. If PHP is installed as an apxs Apache module, then you will need have an additional install of PHP. Luckily, this is easy. Download PHP, & enter the source directory & just run "./configure" with no options. This will configure PHP to install binaries under "/usr/local/lib/" and will not touch your existing Apache installs. Windows users would just have to make sure the PHP install dir and php.exe is in your path.

I'm running this script on a Unix system, so I can make use of an excellent utility called wget, which is a command line utility to download files via HTTP and is very configurable.

On with the script!


// Define the path to the files you want to download from
$dirpath="/usr/wget/mydir/audio/";

// Options for wget may vary.. use wget –help
$command="wget -o errors.wget -c ";

// List all files in the dir. Swipped from php.net
if ($handle = opendir($dirpath)) {

// Recurse over $dirpath & get a pointer to each handle
while (false !== ($file = readdir($handle))) {

// Check its a file, and match to your pattern
// (if you have multiple file types in the dir)
// fnmatch() is available only in PHP >= 4.3.0
if (is_file($file) && fnmatch("*m3u", $file)) {

// file_get_contents PHP >= 4.3.0
$url=file_get_contents($file);

// Be careful using system() in web scripts
system($command.$url);
}
}

// Don't forget to close the handle :-)
closedir($handle);
}

Notes: This is run from the command prompt by: php filename.php
This invokes the PHP interpreter & runs the script.

Its a simple script, but could easily be adapted to renaming a large number of files, displaying the contents of many files, etc. PHP has pretty good string & file handling functions (and getting better with each release).

For hardened Perl users, PHP looks in its infancy, but for simple system tasks, why not use your PHP skills?

8 Responses to "PHP at the command prompt"

1 | Gregory

March 15th, 2003 at 1:54 am

Avatar

I actually just started using PHP at the command line level too. It is great when you want to have scripts that build pages. But I've been using it for some general scripting also.

Also. if your system doesn't have wget on it, you shoudl check forl 'curl' (which I like a bit better myself).

2 | michel v

March 30th, 2003 at 9:10 pm

Avatar

You may want to add that PHP command line scripts, much like Perl scripts, can have a shebang line.
On most Unix/Linux systems, this would be: !#/usr/bin/php -q
Using the shebang line is useful when you set the script file to be executable (chmod +x). This way you can also get rid of the .php extension and pretend that the script is just another executable on your system: instead of typing php ./filename.php you'd just have to type ./filename.

And then you could have made use of PHP's handling of command line parameters, instead of relying on editing the file to change the path. :)


Months ago, I made a Blogger-API compatible command-line blog app in PHP, named blog2.
(I'd give a link, but my webhost is currently down so I'll just give the link in a later comment.)

To post "hello world", I only needed to type blog2 p "hello world" and then it would magically send an XMLRPC command (using IXR) to post that on my blog and give me the new post's ID (for this example we'll say 50). To edit that post I could type blog2 e 50 "new content".
And then there's more than just replacing content, I could append/prepend content to a post. To append I used e-, to prepend I used -e. Exemple:
blog2 -e 50 "\nlook, a new line".
Moreover, that app could also transform your quotes into elegant/smart quotes via calling a little Perl script.

…Just a proof of concept that PHP on the command line is pretty fun. :)

3 | dave

March 31st, 2003 at 7:34 am

Avatar

michel v is correct for all Unix/Linux users out there… I am *extremely* lazy about adding in the shebang line to my scripts.. probably because I write then discard the scripts most of the time.

If you write scripts you are gonna use more than a few times, throw in the shebang line & save yourself some typing (the equivilent in Windows would be to associate the .php extention with php.exe)

Good trick

4 | michel v

March 31st, 2003 at 1:22 pm

Avatar

My bad, I misspelled the shebang lines.
For Unix/Linux: #!/usr/bin/php -q
For Windows: #!C:/php/php.exe -q
(Of course the paths may be different, it all depends on where PHP is installed.)

And then for further information, nothing best than a page from the official documentation! :)
http://www.php.net/manual/en/features.commandline.php

5 | Ihab Hassan

April 14th, 2003 at 1:44 am

Avatar

I need to call php.exe to from a perl script that I wrote.

Basically perl will open a file, print some text and then want to execute php to execute another php file that also open the same file created by perl earlier, print more text and then close.

The final result is a final file with the output from both the perl and the php. My codes are below:-

PERL

#!D:/FoxServ/perl/bin/perl

use strict;

my $message="Thank you for uing BuyPhoneCardsOnline.NET\n\nYour Order will be delivered shortly to the EMAIL provided on your Order\n\nPlease contact us at (214) 697-3947 with any issues\n\nWe look forward to doing business with you in the near future\n";
my $message2="Thanks";

# Save the message into a text file. The text file is automatically deleted by blat.
open (messtxt1,">D:/FoxServ/perl/bin/php/message.data");
print messtxt1 ($message);
close messtxt1;

# Punch out to run the PHP file so that to incorporate the PIN information
my $php="D:/FoxServ/perl/bin/php/php -q final.php";

# Now execture PHP to include PIN
system($php);

PHP

<?php

$fp = fopen("message2.data", "a") or die("Couldn't create new file");
$numBytes = fwrite($fp, "\r\nLook, it's a brand new line!");
fclose($fp);

echo "Wrote $numBytes bytes to the end of newfile.file!";

?>

6 | WE ARE HUGH

March 11th, 2003 at 11:09 am

Avatar

PHP at the command prompt
scriptygoddess: "I'm running this script on a Unix system, so I can
make use of an excellent utility called wget, which is a command line
utility to download files via HTTP and is very configurable."

7 | Raging Platypus

March 11th, 2003 at 8:18 pm

Avatar

PHP at the command prompt
scriptygoddess: "I'm running this script on a Unix system, so I can
make use of an excellent utility called wget, which is a command line
utility to download files via HTTP and is very configurable."

8 | Satyajeet

December 22nd, 2004 at 8:15 am

Avatar

Michel V,
were you able to get your blogpost to have a title??
The blogger.newPost method of the blogger API doesnt let you specify a title to the post
Thanks

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements