Php exec () error
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()
source to share
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);
}
source to share