Php exec php string

Функции запуска программ

Открытые файлы с блокировкой (особенно открытые сессии) должны быть закрыты до выполнения программы в фоне.

Смотрите также

Эти функции также тесно связаны с оператором обратные кавычки («).

User Contributed Notes 40 notes

If you are chrooting php into enviornment that doesn’t have /bin/sh you can’t execute any command! Even when you call
mail() and it calls sendmail . well actually sendmail NEVER gets called because /bin/sh is not in the chroot!
SO in conclusion: YOU MUST HAVE /bin/sh TO EXECUTE SOMETHING.
VERY IMPORTNAT!
I’ve lost few days while find this, hope it helps someone.

The only syntax I found to work for the command portion of an an exec() call on a Win2K devel platform is:

$cmd = «\»path-to-exe\» args file-name»;

where ‘path-to-exe’ has escaped double quotes, args are in the standard format, and ‘file-name’ has any backslashes escaped.

$cmd = «\»C:\program files\winzip\wzunzip.exe\» -c C:\\temp\\uploaded_file.zip»;

Note that the backslashes are escaped in the uploaded_file name, but not in the path to call the Winzip executable. Go figure!

To clarify even more about what has been said in the last few posts:

«exec», «backticks», «system» and so on will fail on Windows 2003 by default.

You must modify the security on cmd.exe to give the user account IUSR-computername the necessary permissions which are at least read & execute.

For those who want to execute a .php script to run in the background, from a
.php web page.

exec(«php script.php parameters 2>dev/null >&- /dev/null &»);

Where.
— php is the path to your php script executer (php has to be specifically complied to be to do this)
— script.php is the script
— parameters are none or more parameters
— 2>dev/null redirects the stderr to a file
— — >&- switches off the stdout
— >dev/null redirects all other output to the file dev/null
— & direct script to run in background

This is the only way I managed to get it working (Linux & Apache, php 4x enviroment) . What is also great is that the process is forked so even though a user may close the browser that initiated the exec(), the process will still run to completion. And the dev/null file can be turned into a log.

What I found odd is that the script I was execing would run fine in the bg when executed from the command line, but not from the browser until I closed the stdin and stdout.

at LAST! tenacity pays off, days trying every little thing!

I didn’t want batch files. I’m trying to use PHP to get rid of batch files.

I didn’t want to call a file to parse the parameters to call a shell to call a file. I’ve got «systems» right now with batches tiered three and five deep.

I just wanted to run stuff.

CALL, tested on WinXP, will be testing on more OSes right away, in PHP4 and 5, with exec, system, works with popen, and passthru.

here is my lame sample function.

// CreateZip
function createzip ($target, $archive)
< $ziputil = "call \"c:\\Program Files\\7-zip\\7z.exe\"";
$archived = escapeshellarg($archive);
$targeted = escapeshellarg($target);

$shellcommand= $ziputil.» a -tzip «.$archived.» «.$targeted.»\n»;

// all of the below are working in Win XP Pro
passthru ($shellcommand);
exec ($shellcommand);
system ($shellcommand);
shell_exec ($shellcommand);
$proc= popen ($shellcommand, «r»); //$proc contains output

LONG PATH NAMES WITH SPACES IN THEM ON WINDOWS!

all in a big long concatenated command line with multiple quoted-filename parameters

Note on XP users: XP-Home edition does not allow to set rights directly on files and folders. You should use cacls command-line utility to do this.

cacls c:\windows\system32\cmd.exe /E /G IUSR_ADMIN2003:F

gives IIS user full access to cmd.exe (potential security hole!), so PHP can fork and execute external programs.

The note is about usage of exec() under windows.

In replay to k dot mitz dot NO_SPAM at att dot NO_SPAM dot net: why not enclose your $cmd in simple quotes instead?

Here is where i’m at right now (also TRYING to use exec() and passthru() on windows XP sp2):

$cmd = ‘»C:\my path with spaces\targetapp.exe» C:\mypathnospaces\targetfile.xxx’;
exec($cmd);

Or, you can put your script into your directory C:\my path with spaces\, and work with php directly from there:

$cmd = ‘targetapp.exe «C:\my other path with spaces\targetfile.xxx»‘;
exec($cmd);

The above also works, provided of course your script has the correct working directory.
________

But. In your cmd.exe, you can issue:

«C:\my path with spaces\targetapp.exe» «C:\my other path with spaces\targetfile.xxx»

Although the above works perfectly in the cmd, the following php script does NOT work on my system:

$cmd = ‘»C:\my path with spaces\targetapp.exe» «C:\my other path with spaces\targetfile.xxx»‘;
exec($cmd,&$content);

Not that it sends an error, it just does nothing, and you get nothing written into your $content array. Not that I do understand why.
________

As far as my few tryings go, the root of the problem lies in the fact that the command to execute has two passages enclosed inside double quotes. Another command which I’ve had working in the cmd but NOT with php is:

‘»C:\my path with spaces\Apache2\bin\Apache.exe» -w -n «Apache2» -k restart’

Maybe someone has a solution; I don’t (other than using chdir).

If you plan to start programs on the server that show message boxes (things that require OK from the server-side), or remain (like notepad.exe), and the exec-command seems to go into an deadly loop, then MAYBE your program has started, but you can’t see it. Because the web server runs as an system process, and it isn’t allowed to interact with the desktop.

To solve a part of the problem (to see the programs you execute), in the control panel in Windows, goto Administration->Services, right-click the server-service, goto Properties and on the second tab (login?) and check the box about allowing the service to interact with the desktop. Click OK. Restart the webserver, and MAKE SURE it is restarted for real (i.e. look in the task manager so the service goes _down_), otherwise the desktop-option won’t take.

Next phase would be to stop PHP from waiting for the processes to complete (this is what makes PHP «loop»). I solved this by creating a small Delphi-application that took the path to the file I wanted to execute and executed it by the WinExec API, which just starts the childprogram but doesn’t wait for it to complete = the program completes and the PHP.exe completes the script. Problem solved!

Delphi-snippet:
WinExec(PChar(),SW_SHOW); // replace with the program path.

I was stuck for about an hour and a half last night trying to get Perl to pass back values to my PHP script after running an exec() command.

Since my Perl script was using STDOUT (after being passed command lines variables), I expected the variables used in my .pl script to be accessible in PHP, no questions asked.

Of course, this wasn’t working and I finally figured it out with the help of a friend (Shawn = superstar): you need to echo the exec() command in order to get the values back into PHP.

Since I was returning multiple values, I added pipe delimiters in my .pl script and then used PHP to parse the string to retreive my data into different variables.

#hit the .pl script with the data
$mydata = exec(«script.pl ‘command line args’ «);
exec(«exit(0)»);

#parse the value that comes back from the .pl script
list($strA, $strB) = split (‘[|]’, $mydata);
echo «valueA: » . $strA.»
«;
echo «valueB: » . $strB.»
«;

In case you ever had to chain from php to another program (e.g. with a cgi php that only gives part of the output, or with php-gtk), here is a little C program that kills his parent (php, for instance), then launches a program given in argument.

int main(int argc, char**argv) /* kill the parent */
kill(getppid(), 15);
argv++;
/* then launch the new program */
return execvp(argv[0], argv);
>
(compile with gcc -O3 -o chain chain.c)
then in php, use
exec(‘chain sh -c echo test’);
?>

Just following up my previous post, in reply to lancelot—du-lac at hotmail dot fr:

The behaviour described was fixed in PHP 5.3 in the following commit:

To replicate the fix in your own code, so it also runs on PHP 5.2, instead of:

$cmd = «. any shell command, maybe with multiple quotes. » ;
exec ( $cmd );
?>

use

$cmd = «. any shell command, maybe with multiple quotes. » ;

exec ( $cmd );
?>

This replicates the behaviour of PHP 5.3 and above. Note that this applies to all shell commands (not just exec(), as used in my example).

Within Linux, when calling the php interpreter directly from popen, or other program execution function to execute a script, it appears as though the script perpetually fails and re-executes.

i.e.

As each re-execution causes the executing script to load under a new PID, it is incredibly difficult to identify and kill the process manually.

[One solution is] to ensure the executing script has a valid shebang, and execute permissions. This allows you to execute the script directly

I found this comment on this page:

[begin of quote]Trying to us the following code failed badly with various results: like «unable to fork», «access denied», «empty results», depending on what settings I used, . even though the same code worked from command line on the server itself.

$retstr = exec(‘nslookup -type=mx myhost.com’, $retarr);

Instead of nslookup I believe this would apply to most programs from the \system32\ directory.

I had to learn that the following finally worked:
$retstr = exec(‘c:\php\safedir\nslookup -type=mx myhost.com’, $retarr);

. but only under the listed preconditions:
1: nslookup.exe is placed (copied) in the directory \php\safedir\
2: the directory \php\safedir\ is included in the system PATH environement variable
3: the file cmd.exe is placed in \php\ as listed by other notes above
4: the directory «c:\php\safedir\» is set in the php.ini setting
safe_mode_exec_dir = «c:\php\safedir\»
.. maybe set in php-activescript.ini as well, depending on your system setup.
5: nslookup is referenced by the full path as otherwise the file from \windows\system32\ will be called. This happend to me with empty result due to missing rights!

Hope this helps somebody saving some time and headaches.
[end of quote]

This is just to complicated. Only two things are needed:
1. Specific permissions for the IUSR account for read & execute to the cmd.exe in C:\Windows\System32 directory
2. Specific permissions for the IUSR account for read & execute to the command that’s needed (example: nslookup.exe in C:\Widnows\System23 directory)

With just this two conditions the exec works fine

(This is for an IIS server running on a windows platform)

Источник

Читайте также:  font-size
Оцените статью