I'm writing a webapp for a professor. It is basically a replacement for a csh script he wrote, along with a simple interface that will allow for dealing with input and output files. All the csh script does is automate a bunch of perl scripts.

Anyway, I chose to do it in PHP because it seemed more straightforward than doing it in perl.

So, the problem I'm having is that there is a lot of file creation and deletion, much of which is done within the scripts. When I try testing exec() and shell_exec(), the executed commands don't produce any side effects. I really need these side effects to happen. Is there any way to actually get side effects?

For example

some_php_function("touch foo.out");
echo `ls .`;

produces as output

{a bunch of stuff}
foo.out
{more stuff}

I tried using the perl in php extension, but it isn't compiling, which makes it difficult to actually use :)

Thanks

Are you trying to retrieve the error messages? If so just redirect the stderr to stdout. (using 2>&1)

some_php_function("touch foo.out 2>&1");
echo `ls .`;

No; I'm trying to get something to happen.

I used touch as an example: When you use touch from a shell, there's no output, but there is a new (blank) file in the directory. When I use exec() or shell_exec(), since touch does not return an output for php to receive, nothing happens. There is not a new file in the directory foo.php is executed from.

The question is: how do I get (as an example) the shell command "touch foo.out" to create a file (as it's supposed to) from within the php environment? I know php has methods for creating new output files, but I'm looking for ways to interact with the OS from within php;

So I guess to be more specific: How can I make the shell do anything besides return a value to php? I haven't been able to get any side effects...

Perhaps perl will be a better language to work in?

I'm not sure, as I don't really do too much with internal programs, but maybe try passthru() - it returns the binary data return of the function to the browser.

-Stephen

<?php
system("touch foo0");
exec("touch foo1");
shell_exec("touch foo2");
?>

After running that, foo0, foo1, and foo2 are created.

Why are you just echoing ls? And is it possible you don't have the proper permissions?

Echoing ls was test output, to see if it was there... I don't know why I'm not getting anything out of exec() or shell_exec()... I haven't tried system, however.

Permissions... perhaps...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.