oekroek oekroek - 1 month ago 7
C Question

Calling C Executable using PHP

I have a c file I've compiled with a executable called "run"
And want to run it with a argv[1] of testfile.txt that is in the uploads directory.

This here seems to work, but I don't understand why.

exec(__DIR__ . '/run uploads/testfile.txt',$output);


C programs are run as such:

./run uploads/testfile.txt


Adding a dot to the beginning of the exec() command makes it not work, but I'm running an executable, not a file (not a file run), why does the first example work but the other doesn't?

This doesn't work but it should?

exec(__DIR__ . './run uploads/testfile.txt',$output);


Where
./run
is the c executable, and
argv[1]
is in the uploads directory which is
testfile.txt

Answer

You only need the . when you're running a program without providing a pathname. If the program name doesn't contain /, the shell searches for the program in $PATH. By typing ./run, it tells it to look in the current directory rather than searching for it.

Since you're providing a full path to the program by prepending __DIR__, it won't search for the program. It's not necessary to insert . before the program name in this case, and it's resulting in an incorrect filename. It's adding . to the last directory in __DIR__, and that directory doesn't exist.