Web to cli in self script?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Web to cli in self script?

Post by scuar »

Hello, is it possible to make a web script to self execute itself in CLI mode?

My script has something like this:

Code: Select all

if(isset($_SERVER['argc']) && ($_SERVER['argc'] >= 1))
{
//do something
}
else
{
	if(//nothing set)
	{
	print 'html source + option to execute the script in cli';
	}
}
And to execute itself i use:

Code: Select all

function execCli()
{
	shell_exec("php " . __FILE__ );
}
But nothing happens, so i was wandering after all if it's possible to do this.

Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Web to cli in self script?

Post by Christopher »

scuar wrote:Hello, is it possible to make a web script to self execute itself in CLI mode?
Yes.
scuar wrote:But nothing happens
How do you know that the script does not run?
(#10850)
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Web to cli in self script?

Post by Eric! »

Also, some servers are configured to prevent shell executions. What you posted appears like it should work provided the path to the php CLI is in your environment variables (otherwise specify a path and some switches

so either
shell_exec("php -f" . __FILE__ );
or
shell_exec("/path/to/bin/on/your/system/php -f" . __FILE__ );

Start small with something like

Code: Select all

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
http://php.net/manual/en/function.shell-exec.php
http://www.php.net/manual/en/features.c ... ptions.php

Furthermore you might try exec() and see where it is running from. If it is in safe mode then it will only run things inside the safe mode directory. http://www.php.net/manual/en/ini.sect.s ... e-exec-dir

Code: Select all

<?php
echo exec('pwd');
?>
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: Web to cli in self script?

Post by scuar »

How do you know that the script does not run?
I know it because it should create a new file but it doesn't.
Furthermore you might try exec() and see where it is running from. If it is in safe mode then it will only run things inside the safe mode directory.
Great, i'll try.

Thank you both, i bet it's just a coding mistake.
Post Reply