title: can't download file using wget with authentication while being called from php
body: <p>I've created a small remote download script which downloads remote files to my server using wget.
  Now here's how it works:
  I have a html form, which the user can enter the URL, destination file name, and optionally username and password to authenticate, in case needed.
  The form calls a php script, using AJAX, and the php script passes the information to a shell script.
  Now the problem is, the shell script works flawlessly on my local linux machine, but on my server, it doesn't work with authentication(without authentication, it works just fine.)</p>
  
  <pre><code>#!/bin/bash
  
  if test $# -eq 2
  then
      wget -O "$2" $1
  elif test $# -eq 4
  then
      wget -O "$2" --http-user="$3" --http-password="$4" $1
  else
      wget $1
  fi
  </code></pre>
  
  <p>it might be worth mentioning that, I own a shared server.</p>
  
  <p>Thanks</p>
  
  <p>EDIT:
  Is it possible that the version of wget installed on the server, doesn't support http authentications???</p>
  
  <p>EDIT 2:
  I piped the output of wget to a file like this:</p>
  
  <pre><code>wget -O "$2" --http-user="$3" --http-password="$4" $1 | echo &gt;&gt; output
  </code></pre>
  
  <p>but the output is empty, i.e. no messages is being printed from wget!!!
  And I did this too, to check if the credentials passed is ok:</p>
  
  <pre><code>echo "$3 | $4" &gt;&gt; credentials
  </code></pre>
  
  <p>And it was ok.</p>
  
  <p>And this is the php code which runs the shell script:</p>
  
  <pre><code>if(isset($_POST["startdownload"]))
  {
      list($url, $dest, $user, $pass) = explode("|", urldecode($_POST["data"]));
      $filelen = file_len($url);
      $freespace = getRemainingSpace();
      //die( "( Free: $freespace, File: $filelen )" );
      if($filelen &gt; $freespace - 10000)
      {
          $result = json_encode(array("error" =&gt; true, 
                          "message" =&gt; "There is not enough space to download this file. ( Free: $freespace, File: $filelen )"));
          die($result);
      }
      else
      {
              if($user != "NULL" &amp;&amp; $pass != "NULL")
              {
                  execInBackground("../dl.sh '{$url}' '../{$dest}' '{$user}' '{$pass}'| at now");
              }
              else
              {
          execInBackground("../dl.sh '{$url}' '../{$dest}' | at now");    
              }
              $result = json_encode(array("error" =&gt; false, "message" =&gt; "Starting download..."));
              die($result);
      }
  }
  function execInBackground($cmd) 
  {
      if (substr(php_uname(), 0, 7) == "Windows"){
          pclose(popen("start /B ". $cmd, "r")); 
      }
      else {
          exec($cmd . " &gt; /dev/null &amp;");  
      }
  } 
  </code></pre>