How to execute command for 2 location using PHP exec() - php

I need to create a website that utilizes 2 existing encryption CMD.exe in PHP.
I know that you can use exec() to open CMD file in PHP, but currently my situation is I will need to type like this
D:\temp\userA>d:\myprogfolder\myprog /u r
in my command so that the program will works, in which the D:\temp\userA contains all the file that I need to encrypt and after > is the location of my exe file.
As you can see, they are both in different locations. So for exec(), I don't know how to write a command that can fit into exec() as I try to fill the above line into exec() and it doesn't run.
Is there a way so that I can combine the location of my file that I want to encrypt and exe location into one line and pass it to exec()?
Thank you.

Change the working directory before executing the command:
chdir("D:\temp\userA");
exec("myprogfolder\myprog /u r");

Related

shell_exec in php works good for the default shells commands such as ls but is not working the same with hadoop commands

while using shell_exec in php for example
$make=shell_exec('ls');
echo $make;
it returns the results in our webpage but when we use hadoop fs -ls instead of ls it doesn't return anything does shell_exec works with hadoop shell commands
or is there any other way to do the same i also tried it using the commands in python scripts and then executing those scripts using php's shell_exec but still no good
It may happen that the handoop executable is not in the path of current working directory or context.
Try to execute the command with full path instead.
For example:
instead of using command like: shell_exec('hadoop fs -ls');
use shell_exec('/usr/bin/hadoop fs -ls');
I don't know the exact location of handoop executable on your system. use mlocate utility to find the exact file location.

Is it possible to pass the parameter in php execution?

Hi I am trying to run a C binary program in a php script.
The name of the binary program is prog and it takes one or two parameters. In terminal this binary program runs fine with this command:
prog param1
In a php sript, I am trying to run the above command. But I am not sure if this syntax is correct. I have the following:
exec('../permission/prog param1', $output, $return);
I am not seeing expected behavior after executing the php file. Is it possible to pass the parameter like this in php?
Thanks!
I think you will need to do couple of things and check again:
Your function params uses is fine but try using the full script path.
If still it does not function from with in your php script. You will need to check and see if the apache group:user has the ownership of running the permission/prog script or not. If not try giving the ownership to apache group:user. The apache group:user may apache:apache. You will need to exactly check for what group and user is there in your server for apache.
PHP usually runs executables from the user and group of www-data
This will likely be different to the user you are using in the terminal. Check that www-data has permission to execute the binary

Using a single command line through php

I am trying to run a c++ executable that I have on my computer through my PHP interface. I do not have admin rights on the computer that my PHP is being hosted from and therefore cannot add the appropriate items to my PATH for my c++.
I am able to call the exec function in PHP and run my program via a batch file. In my batch file I am able to set my path variable to what I need it to be.
Here is what I am currently doing (with runVideoparser being the batch file):
exec("C:\\Users\\hewittjc\\Desktop\\runVideoParser", $output, $return);
echo "Program returned $return.";
The above works just fine when passing a fixed parameter to my c++ in the batch.
However, my issue is that I need to be able to pass the program different arguments each time. If I run it via batch file then I loose this ability.
So my question is, How can I:
1) Open a command prompt via PHP (I'm running windows)
2) Set my path variable in the prompt via PHP
3) Then, using that same prompt to preserve the path variable, run my program passing it the required argument.
I suppose I could generate the batch in my PHP then run that, but I am seeking any more elegant solutions.
In theory, you can do something like this:
$old_path = getenv("PATH");
$new_path = "/my/additional/path:$old_path";
putenv("PATH=$new_path");
system("command -with flags -and args -and such");
However, this won't work if the host machine has Safe Mode on and PATH is not in the safe_mode_allowed_env_vars directive.

Can PHP execute a terminal command [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Executing linux commands with PHP
I have a PHP file that creates an .xfdf file I am wondering if PHP has a way of executing a command like you would type into the terminal window? I have to run the following command when after the .xfdf file is created to generate the .pdf file
pdftk /var/www/pdf/TimeCard.pdf fill_form /var/www/pdf/results/Brandon_01_23_13.xfdf output /var/www/pdf/results/testNew.pdf flatten
The command would need to be able to accept a PHP variable so its customized for the user. Is there an easy way to do this?
Yes, you can use exec(), pcntl_exec(), system().
$command = 'pdftk /var/www/pdf/TimeCard.pdf fill_form /var/www/pdf/results/Brandon_01_23_13.xfdf output /var/www/pdf/results/testNew.pdf flatten';
exec($command);
Be very careful when using these commands, they are very useful/powerful, but because of this a hacker could potentially gain access to your whole server through it (depending on permissions, but it's more than enough to compromise your site).
If you need to send user uploaded file names/text, check them to the blood (for example check if the file path is in the proper place, and not ../../../something.pdf, check MIME types and extensions).
Yes, you can use:
system
exec
passthru
Depending on your exact requirements, you can use exec(), system() or shell_exec()
Use the system() or eval() functions.
But make sure the user can not inject arbitrary commands!

Run shell commands with PHP?

Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig

Categories