Location: PHPKode > scripts > WGetAgent > wgetagent/documentation.html
<br>
Requirements: <br>
- linux so<br>
(doesn't work on M$ Windows);<br>
- wget properly installed;<br>
- rm binary file or script in PATH env - to delete unwanted session files;<br>
(or you can work around it and delete them from outside this class)<br>
- exec php function able to be called<br>
- write (and read) rights on destination directory<br>
<p>Short note:<br>
  I tried to obtain the name of downloaded file from wget (as download url can be a script eg. http://www.somewhere.com/download.php?id=x)<br>
  A workaround this was downloading the file to a directory named with session id (to be sure it's unique) and downloading file there.<br>
  After download ended I deleted all log files and read files in that directory to get name of downloaded file.<br>
</p>
<p>Note: Note that in example script I used /tmp directory for simplicity. You can change that as you please.<br></p>
<p>Defined variables:</p>
<p>// variables that can be used agains get_error()[&quot;error_no&quot;]<br>
  // this is useful to understand the code better<br>
  // (get_error()[&quot;error_no&quot;] == $WGET_ERROR_CANNOT_RUN vs get_error()[&quot;error_no&quot;] == 1)<br>
  $WGET_ERROR_ALL_OK = 0;<br>
  $WGET_ERROR_CANNOT_RUN = 1; // cannot find exec function<br>
  $WGET_ERROR_WGET_NOT_FOUND = 2; // wget executable not found in current path<br>
  $WGET_ERROR_DEST_NOT_FOUND = 3; // destination folder not found<br>
  $WGET_ERROR_DEST_NO_RIGHTS = 4; // no rights to write in destination folder<br>
  $WGET_ERROR_STORE_FUNC = 5; // user defined store function not found<br>
  $WGET_ERROR_SCRIPT_ERROR = 6; // script that runs after download not found or not executable<br>
  $WGET_ERROR_RM_NOT_FOUND = 7; // script that runs after download not found or not executable<br>
</p>
<p>Internal variables:</p>
<p> var $wget_url; // url to connect to<br>
  var $wget_user, $wget_pass; // user/pass to connect to ftp server<br>
  var $wget_prog; // location of wget binary executable...<br>
  var $wget_destination; // location where wget will put the file<br>
  var $wget_session; // Session id in which wget runs (unique string)<br>
  var $wget_limit_rate; // max download rate for this wget session (default is in bytes, but u can specify Kb: 20K)</p>
<p> // log files prefix... During download wget and wget_agent class generates log files.<br>
  // this variable holds log files name prefix (default wga)<br>
  // (useful if we want to delete only logs, leaving result file in place - read the Short note above)<br>
  var $wget_log_files_prefix;</p>
<p> var $cleanup_logs; // wheather to clean all files created at destination after download (0/1)</p>
<p> // Store function will get as parameters:<br>
  // function my_wget_store( $session_id, $url, $dest_dir )<br>
  // session_id: string that identifies the session id of current wget execution<br>
  // url: from where did the download start<br>
  // dest_dir: where control and download files are<br>
  // also there can by used methods from within other classes. see the example for an... example...<br>
  var $wget_store_function; // user definded function that will store session id into table/file<br>
  var $wget_store_function_class; // useful when u want to run methods from within other classes...</p>
<p> // a script that will be run AFTER download is complete<br>
  // this can be a php script or a bash script (I use &quot;/path/to/php /path/to/finish_download.php&quot;)<br>
  // after this string class will add &quot;wget_sessid=&quot;.$this-&gt;wget_session - passing as parameter wget_sessid holding session id<br>
  // parameter string is linked to script string using $wget_link_param_char (read bellow)<br>
  // in this script you can look into download tables to update download state and location to the resource file that was downloaded<br>
  var $wget_script; <br>
  // script directory. B4 running the script directory will be changed at this location<br>
  var $wget_script_dir;<br>
  <br>
  // character that links parameters for given script<br>
  // by default it is &quot; &quot; (CGI or normal linux executable), but if u use a PHP script and want parameters passed as a url query u might use &amp; as separator<br>
  // be sure the link char is shell executing safe<br>
  var $wget_link_param_char;</p>
<p> // holds error number (see variables above) and error message<br>
  var $error_no, $error_msg;<br>
</p>
<p>Methods:<br>
    <br>
  // &quot;constructor&quot;<br>
  // url: url of file (or script that provides the file) that will be downloaded<br>
  // destination: absolute path where file will be stored<br>
  // limit_rate: tell wget to run at a certain transfer rate (optional)<br>
  // user, pass: user and password needed to access url file (optional)<br>
  // Method will make requirements tests and set $error_no, $error_msg accordingly<br>
  wget_agent::wget_agent( $url, $destination, $limit_rate = &quot;&quot;, $user = &quot;&quot;, $pass = &quot;&quot; );</p>
<p> // user-control wget session store function<br>
  // store function will be called after download is started in background<br>
  // method checks if function or method is &quot;viewed&quot; by class<br>
  // func: function name<br>
  // class: class that has that method (see example)<br>
  // Affects: $this-&gt;wget_store_function, $this-&gt;wget_store_function_class<br>
  function wget_store_function( $func, $class = &quot;&quot; );</p>
<p> // absolute path to a script must be given, this script will run after the download is completed<br>
  // script: location of the script (absolute)<br>
  // sep_char: this char (string) separates the script location and the parameter wget_sessid when calling the script<br>
  function wget_script( $script, $sep_char = &quot; &quot; );</p>
<p> // class will switch to this directory before running the script, but after download is complete<br>
  function wget_script_dir( $dir = &quot;&quot; );</p>
<p> // if there is a previous session started that must be continued use this method so class will use old logs<br>
  // this method must be called right after the class is intited<br>
  function wget_session_id( $id = &quot;&quot; );</p>
<p> // 0/1 (true/false) whether to delete or not wget log file after download finishes<br>
  function wget_cleanup_logs( $clean = -1 );</p>
<p> // 0/1 (true/false) whether the transfer will be passive or not<br>
  function wget_passive( $passive = -1 );</p>
<p> // by default store function takes as parameters session id, url source and destination directory<br>
  // if we want to pass an extra parameter (an object or an id) we pass it here<br>
  function wget_extra_parameters( $parameter = &quot;&quot; );<br>
  <br>
  // we can prefix log files' names (default is wga)<br>
  // so all log files name will start with wga (useful if we want to delete only logs, leaving result file in place)<br>
  function wget_log_prefix( $prefix = -1 );<br>
  <br>
  // This method starts the download<br>
  // It creates an execution script and executes it in background, returning the control to the script.<br>
  // also, after returning control to the script, it calls the store function (if provided)<br>
  function wget_run();</p>
<p> // returns an array with error number and error message<br>
  function get_error();<br>
</p>
Return current item: WGetAgent