0

I am writing php script, which will be used for making sites from "standart" site. There's a lot of unix shell commands, and I've found the problem with displaying errors.

Example: I need to check that the site folder doesn't exist yet.

$ls_newsite = exec('ls /vhosts/'.$sitename, $output, $error_code);
if ($error_code == 0) {
    Shell::error('This site already exists in /vhosts/');
}
Shell::output(sprintf("%'.-37s",$sitename).'OK!');

So, I can handle error, but it will be display anyway.

php shell.php testing.com

Checking site...
ls: cannot access /vhosts/testing.com: No such file or directory
testing.com.................................OK!

How can I prevent displaying? Thanks

3
  • As @Colin Morelli said, you don't need to use console commands for this. file_exists will suffice. Commented Mar 5, 2013 at 11:26
  • It's just example to show problem. Commented Mar 5, 2013 at 11:55
  • But even so, there aren't very many cases you should be using the exec command. Commented Mar 5, 2013 at 11:56

1 Answer 1

1

You don't need the output from these CLI calls, just the error code. So direct your output to /dev/null (otherwise PHP will print whatever goes to stderr unless you use proc_open and create pipes for each of these - overkill).

$ls_newsite = exec('ls /vhosts/' . $sitename . ' > /dev/null 2>&1', $output, $error_code);

That will work without giving you any output.

Now, on to a few other issues:

Use escapeshellarg for anything you're passing to a shell command. A better way to write this same code is:

$ls_newsite = exec(sprintf('ls %s > /dev/null 2>&1', escapeshellarg('/vhosts/' . $sitename)), $output, $error_code);

Be 100% sure that you need to use console commands. There are PHP equivalents for most file-based console commands (stat, file_exists, is_dir, etc) that would make your code both more secure and would allow it to be platform-independent.

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

Comments

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.