0

I was trying to get the following linux command to work using PHP. I read about the shell_exec command in PHP, that can be used to execute shell commands.

I am trying to output some simple text to a text file. I tried the following:

<?php
    $cmd = 'echo "hi" > Desktop/test/output.txt';
    shell_exec($cmd);
?>

Please note that, I am running this on Ubuntu 16.04. I have also made the www-data the owner of the Desktop/test folder so that it can write to it. The following is the command I used for that:

sudo chown -R www-data:www-data Desktop/test

When I execute the above command, directly in a shell I can see an output.txt file created with the string output.

However, when I execute the php script through apache web server, I cannot see the output.txt file being created.

Glad if you can point me towards the right direction.

Thanks

4
  • Does it work, when you use an absolute path? Commented Aug 1, 2017 at 9:04
  • Try getting the output of the command to see if there is any error Commented Aug 1, 2017 at 9:05
  • 1
    try use absolute path: /home/yourUserName/Desktop/test/output.txt Commented Aug 1, 2017 at 9:06
  • You need to use absolute path Commented Aug 1, 2017 at 9:13

1 Answer 1

1

As I said in the comment: you should use an absolute path.

<?php
    $cmd = 'echo "hi" > /home/username/Desktop/test/output.txt';
    shell_exec($cmd);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This was a bit weird how the command worked without absolute path directly on the shell, but not through PHP.

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.