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?

Community
  • 1
  • 1
Vipin Kumar KM
  • 356
  • 5
  • 17
  • 1
    The default method in PHP to load external files is to use [cURL](http://php.net/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](http://php.net/exec). – feeela Dec 04 '14 at 10:32
  • yes, but people can remove cURL right? That's why I thought to stick with `exec` function – Vipin Kumar KM Dec 04 '14 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. – feeela Dec 04 '14 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 :) – Vipin Kumar KM Dec 04 '14 at 10:44

1 Answers1

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.

Andrei Nemes
  • 3,062
  • 1
  • 16
  • 22
  • 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? – Vipin Kumar KM Dec 04 '14 at 10:38
  • `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`. – feeela Dec 04 '14 at 10:40
  • 1
    Sometimes all you need is a good old read of the docs :p Assuming they have any – Andrei Nemes Dec 04 '14 at 11:47