0

I have a bash script and now I need to convert it into php, as a cgi script.See I've a bash line like this

wget http://repo.domain/sources/latest_version.tar.gz > /dev/null 2>&1;
if [ $? != 0 ]
then
echo "Could not download file...";
exit 0;
fi

If I run the wget using php system/exec, is there a way to catch the exit status like $? used in this script? I have read that file download can be done using file_put_contents() as here in this stackoverflow post.But I'm also concerned about the allow_fopen_url Off part as one of the comments mentioned.I think I need a more universal solution keeping the changes from bash to php minimum.

How can I catch the exit status here ? Any hint please?

4
  • 1
    The default method in PHP to load external files is to use cURL. Don't ever use file_get_contents() to access something that is not on your own server. This may break due to configuration. And yes you can get the return value of executed shell commands in PHP. But that is already well described in the PHP manual. Commented Dec 4, 2014 at 10:32
  • yes, but people can remove cURL right? That's why I thought to stick with exec function Commented Dec 4, 2014 at 10:35
  • Yes and people could also remove PHP. There is difference between securing your server through configuration or randomly removing libraries. In this case cURL is just a dependency as any other lib that your program requires to run. Commented Dec 4, 2014 at 10:39
  • Didn't mean to offend friend.I'm trying to create a control panel extension for panel admins.I can't be sure about what they think about cURL :) Commented Dec 4, 2014 at 10:44

1 Answer 1

2

There is an exit function. From php documentation:

void exit ([ string $status ] )
void exit ( int $status )

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

exit is a language construct and it can be called without parentheses if no status is passed.

So in your case it would be exit(0)

Edit: Well, as far as I see it, wget is a linux shell command not php, you can probably run it and get the output?

exec('wget http://repo.domain/sources/latest_version.tar.gz > /dev/null 2>&1', $output, $return_var);

Again, according to the docs:

return_var If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

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

3 Comments

Thank you for the response.I have seen exit,But I'm not clear.Wouldn't it stop the execution of rest of the code?
exit halts the current PHP script and returns an exit code to the calling instance. But the question is about how to get an exit code that was returned by another program, like wget.
Sometimes all you need is a good old read of the docs :p Assuming they have any

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.