Question

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.

Was it helpful?

Solution

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()

OTHER TIPS

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]' );

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

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

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);
}

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

Try exec("mkdir $somevariable");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top