0

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

4 Answers 4

1

Suppose __DIR__=='/foo/bar'.

Then, __DIR__ . './run' would evaluate to /foo/bar./run which does not exist.

Sign up to request clarification or add additional context in comments.

2 Comments

So how would I fix it using the dot?
@oekroek Leave out the dot, it's not needed.
0

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.

Comments

0

The dot concatenates two strings.
__DIR__ is a magic constant which is your php file directory not slash-terminated (see php doc).
Suppose __DIR__ = my_dir, if you add a dot before run you'll get:

my_dir./run

Comments

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

__DIR__ has no / at the end

So you will try to execute /home/me/project./run.

You could run /home/me/project/./run though for example. The dot . refers to the current directory.

2 Comments

I'm not sure what you mean by it has no / at the end?
if you directory name is my_dir then _DIR_ = my_dir and not my_dir/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.