Link to home
Start Free TrialLog in
Avatar of arunhem
arunhem

asked on

PHP:How to connect to a unix box, run a script and ftp back the generated file to local box

I want to be able to connect to a unix box from my windows machine where I am running my PHP script. I would like to run my perl script on the unix box which generates a text file. I would then like to ftp file back to my local box for display on the web site. How can this be done in php.
Avatar of AndyAelbrecht
AndyAelbrecht

the perl script has to be web-accessible if you want it to be run by a remote host.
one of the things you can do is put it in your cgi-bin directory.
the text file it generates should be dumped (ofcourse) in an ftp-readable directory or (this is easier, imho) it could put it somewhere in one of your web-accessible directories.

depending on the option you choose (i'm assuming you want ftp):
ftp:
<?php
  $ftpconn = ftp_connect($ftpserver); //$ftpserver = IP or FQDN
  $ftplogin = ftp_login($ftpconn, $ftpuser, $ftppass);
  $fp =  fopen("path_to_where_file_has_to_be_saved", "w");
  if ($ftpconn && $ftplogin) { //both are booleans
     if (ftp_fget($ftpconn, $savefilepath, "remote_filename", FTP_ASCII)){
       echo "File successfully downloaded";
     }
  }
  ftp_close($ftpconn);
?>

(if you saved the file on a webaccessible server, it'd be something like this:)
<?php
  $remotefp = fopen("http://servername/filename.txt, "r");
  $fp = fopen(("path_to_where_file_has_to_be_saved", "w");
  do {
    $data = fread($remotefp, 4096);
    if strlen($data) == 0) {
      break;
    }
    fputs($fp, $data);
  }
  fclose($fp);
  fclose($remotefp);
?>

hope this helps,

cheers,
Andy
oops: to run the remote cgi-script:

<?php
  $runscript = fopen("http://servername/cgi-bin/script.pl");
  fclose($runscript);
?>

should do it afaik :-)
Avatar of arunhem

ASKER

So I would run first run my perl script and then do the ftp. So there is just no way to do without accessing this perl script through URL. I mean doesn't PHP provide anything that would allow you to run something like SSH and then run the script at the  remote server and then run the ftp to get the file back?
ASKER CERTIFIED SOLUTION
Avatar of AndyAelbrecht
AndyAelbrecht

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial