I have a script which is running on Apache and also can be executed from command line.
How can i know, on what is php script running, apache or command line?
There is a constant build in PHP that you can use PHP_SAPI. If you are on the commandline the value of this constant is cli than you are on the command line. Every other value like cgi, cgi-fcgi, etc.
Why not add a parameter and only pass it when invoking from the command line?
http://php.net/manual/de/reserved.variables.argv.php
Create a page upload it and browse to it.
See page contents below.
<?php phpinfo(); ?>
Related
Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php
I want to execute a command in ubuntu terminal. When I directly run the command in terminal, it runs without any problem. But What I actually want to do is to execute this command via PHP.
chdir('/home/thilini/FYP/testone/bin/');
exec('./mindtct input_folder/filename output_folder/filename');
The php code I wrote is shown above. I am using ubuntu 10.10 and the LAMP configuration. chdir is working fine and I have successfully moved from /var/www/ to /home/thilini/FYP/testone/bin/ (where I have the executable mindtct). But exec is not working. (mindtct is an executable which convert the file in the input folder to another format and store it in the output_folder under the given name).
What am I doing wrong?
The problem was an issue in the path. A forward slash was missing.
If you're running below php 5.4,check "safe_mode" in your ini file.
http://www.php.net/manual/en/features.safe-mode.functions.php
You probably want
exec('./mindtct input_folder/filename output_folder/filename');
Maybe you should set error_reporting(-1) in your script so you get some errors
You want to use shell_exec(), not exec().
shell_exec() executes a command in the terminal, whereas exec() opens an application.
$results = shell_exec('./mindtct input_folder/filename output_folder/filename');
print_r($results);
This will execute the command, store it in results, and then print_r the results in array format.
http://php.net/manual/en/function.exec.php
http://php.net/manual/en/function.shell-exec.php
I have a PHP CLI script that I invoke using
php application.php --args etc
However I would like to alias the script so that I can just execute the script without prefixing the command line call with php and having the '.php' extension.
application --args etc
Is this possible? I pressume it is but lack the knowledge or probably the correct terms to search for in Google.
You need to do the thing that Mike Brants says add the next line to your sample.php file
#!/path/to/cli/php
but also you have to do these in linux
chmod +x sample.php
To tell the linux (unix) machine to interprete these file as an excecutable
You could just use a shebang to define the application to use for execution from within the file. So at the beginning of your script you would place something like this:
#!/path/to/cli/php
<?php
// start your PHP here
When executed from command line the OS will know to use the specified PHP CLI application to execute the script. Obviously the path to the PHP CLI excutable will vary based on your system and should be substituted with what I have shown above.
This is more flexible that aliasing IMO, as you don't need to enter an alias for each PHP script you may want to run in such a manner from the command line.
ahah. alias can be added to the .base_profile
http://www.hypexr.org/bash_tutorial.php#alias
Use a so called 'shebang':
In the first line of your script add:
#!/usr/bin/php
where /usr/bin/php is the path to your php cli executable.
That's it !
Even more logical and pleasant (at least my favorite) call is #!/usr/bin/env php
Quote part from the user contributed note on PHP manual it self:
uses "env" to find where PHP is installed: it might be elsewhere in the $PATH, such as /usr/local/bin.
How can I run a php file without opening in browsers? Can I do that with php? or Should I use other languages?
i.e. sending a birthday mail to users otomaticly
i.e.2 send a ping every 5 hours etc
for automate your scripts you should use cron
I'd use python for it.
However, PHP works perfectly fine for this purpose. It has the cli SAPI which is meant for commandline scripts (including cronjobs etc.).
Simply start your PHP file with the hashbang line pointing to the PHP interpreter:
#!/usr/bin/php
<?php
echo 'hello shell';
CLI is command line interface. And from cron you can start your PHP scripts through command line.
Try this to start and you will understand what to do next:
http://www.php.net/manual/en/features.commandline.usage.php
Form you command line run something like
C:>c:\php\php.exe c:\path\to\your\script.php
I am working on Windows server and able to run command from command prompt
c:> %convertxls% {some args....}
But when I run same command from php script
*shell_exec(%convertxls% ..... 2>&1);*
it gives me error as
%convertxls% is not recognized as an internal or external command, operable program or batch file.
I think when I am running command from command prompt, it run for user which logged in. And when I run the php script it run for "www" user for which path is not set.
Can anybody tell me where I am doing mistake?
*Note: I haven't written complete command.
Supply the full path to the executable.
ignacio right,I wanted to add one more point that ignacio did not specify .
Check the parameter disable_functions in the php.ini .
maybe this function is not allowed.
This sounds like the environment variable %convertxls% is not set.
You can use putenv() to set it; alternatively, as Ignacio already says, specify the full path.