tags:

views:

1208

answers:

8

Hello,

I'm having a little problem with the following:

When I execute this line:

echo exec(createDir($somevariable));

I get this error:

Warning: exec() [function.exec]: Cannot execute a blank command in /home/mydir/myfile.inc.php on line 32

Any ideas.

Thanks.

A: 

I'm guessing createDir() doesn't have a return value.

Try exec("mkdir $somevariable");

Max
except that this probably would create a gaping security hole
David Schmitt
Absolutely correct - but to be discouraged. system() and exec() are to be avoided whenever there is an alternative, and PHP has perfectly good internal methods for manipulating the filesystem.
slim
I'm presuming $somevariable is sanitized input here, and answering the question that was asked. Having exec() enabled is a security hole in and of itself.
Max
+1  A: 

What does the createDir method return? I don't see it in the php manual. If you are trying to create a directory why not use mkdir

Mark Davidson
A: 

With exec you can execute system calls like if you were using the command line. It hasn't to do anything with executing PHP functions.

To create a directory you could do the following:

exec( 'mkdir [NAME OF DIRECTORY]' );
okoman
except that this probably would create a gaping security hole
David Schmitt
again, perfectly correct. But by not using PHP builtins, you sacrifice portability and (potentially) security.
slim
I see David and I are navigating around SO in lockstep :)
slim
A: 

You're misunderstanding the purpose of exec(). If all you want to do is create a directory then you should use mkdir().

Ignacio Vazquez-Abrams
The suggestion to use mkdir is correct, but exec doesn't "replace" the current process, it spawns a child process and waits for it to terminate.
Paul Dixon
Ah, you are correct. Yet another instance where PHP pretends that it's smarter than the underlying system.
Ignacio Vazquez-Abrams
Ouch! It's a nasty deviation from the naming convention used by most other languages. PHP's exec() does what other language's system() does.
slim
Well no, because exec() returns the command's output whereas system() doesn't. So it's like some sort of twisted popen().
Ignacio Vazquez-Abrams
Good point. It's like backticks in Perl.
slim
A: 

I'd guess that your createDir() function doesn't return anything. Might also be worth checking that $somevariable is also set to something sensible

Rowland Shaw
+2  A: 

exec() expects a string argument, which it would pass on to your operating system to be executed. In other words, this is a portal to the server's command line.

I'm not sure what function createDir() is, but unless it's returning a valid command line string, it's probably failing because of that.

In Linux, you might want to do something like

exec('/usr/bin/mkdir '.$path);

...on the other hand, you should abstain from using exec() at all costs. What you can do here, instead, is take a look at mkdir()

Henrik Paul
A: 

Thanks a million all.

Ignacio Vazquez-Abrams - I was using exec() cause the dir to be copied is quite large and I needed the process to be completed before running the rest of the script.

tim
mkdir() and others will also wait until they complete before returning control to the script.
slim
A: 

I think I've derived from other posts and comments what it is you actually want to do:

I think createDir() is a PHP function you've written yourself. It does more than just make a directory - it populates it, and that might take some time.

For some reason you believe that the next command gets run before createDir() has finished working, and you thought that by invoking createDir() using exec() you could avoid this.

Tell me in a comment if this is way out, and I'll delete this answer.

It's seems unlikely that createDir() really does keep working after it's returned (if it does, then we call that 'asynchronous'). It would require the programmer to go out of their way to make it asynchronous. So check that assumption.

Even so, exec() is not for invoking PHP functions. It is for invoking shell commands (the kind of thing you type in at a command prompt). As many of us have observed, it is to be avoided unless you're very careful - the risk being that you allow a user to execute arbitrary shell commands.

If you really do have to wait for an asynchronous function to complete, there are a couple of ways this can be done.

The first way requires that the asynchronous function has been written in an amenable manner. Some APIs let you start an asynchronous job, which will give you a 'handle', then do some other stuff, then get the return status from the handle. Something like:

handle = doThreadedJob(myParam);
# do other stuff
results = getResults(handle);

getResults would wait until the job finished.

The second way isn't as good, and can be used when the API is less helpful. Unfortunately, it's a matter of finding some clue that the job is finished, and polling until it is.

while( checkJobIsDone() == false ) {
    sleep(some time interval);
}
slim
slim - your assumptions are correct, I'll take your advice and do without exec(), though I was having problems with the way in which the script was running, e.g it was executing a rename() function before having finishing a mkdir()
tim