Php exec () error

I have a little problem with the following:

When I execute this line:

echo exec(createDir($somevariable));

      

I am getting this error:

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

      

Any ideas.

Thank.

0


source to share


6 answers


exec()

expects a string argument which it will pass to your operating system for execution. In other words, it is a portal to the server command line.

I'm not sure which function createDir()

is there, but if it doesn't return the correct command line string, it probably doesn't work because of this.

On Linux, you can do something like



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

      

... on the other hand, you should refrain from using it exec()

at all costs. You can look insteadmkdir()

+2


source


With exec you can make system calls, for example if you were using the command line. It doesn't have to do anything with PHP function execution.

To create a directory, you can do the following:



exec( 'mkdir [NAME OF DIRECTORY]' );

      

0


source


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

0


source


I would assume your function createDir()

is not returning anything. It is also worth checking that it is $somevariable

also set to something sane

0


source


I think I got from other posts and comments what exactly you actually want to do:

I think createDir()

this is a PHP function that you wrote yourself. It does more than just create a directory - it populates it and it can take a while.

For some reason, you think the next command is being run before it createDir()

finishes, and you thought that by calling createDir()

with exec()

, you could avoid it.

Let me know in the comment if this is an output and I will delete this answer.

It seems unlikely that it createDir()

actually continues to run after it returns (if it does, we call it "asynchronous"). This will require the programmer to get out of the way to make it asynchronous. So check this assumption.

However, exec()

it is not intended to call PHP functions. It is for invoking shell commands (the type you type on the command line). As many of us have observed, it should be avoided unless you are very careful - the risk that you are allowing the user to execute arbitrary shell commands.

If you really need to wait for the asynchronous function to complete, there are several ways to do it.

The first way requires the asynchronous function to be written with amen. Some APIs allow you to start an asynchronous job which will give you a "handle", then do some other things and then get the return status from the handle. Something like:

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

      

getResults will wait for the job to complete.

The second method is not as good and can be used when the API is less useful. Unfortunately, this is a matter of looking for some clue that the work is complete, and polling until that is.

while( checkJobIsDone() == false ) {
    sleep(some time interval);
}

      

0


source


I am assuming createDir () has no return value.

Try exec("mkdir $somevariable");

-2


source







All Articles