Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-06
6
Medium Priority
?
217 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:arunhem
  • 3
4 Comments
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16391648
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
0
 
LVL 4

Expert Comment

by:AndyAelbrecht
ID: 16391690
oops: to run the remote cgi-script:

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

should do it afaik :-)
0
 

Author Comment

by:arunhem
ID: 16391747
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?
0
 
LVL 4

Accepted Solution

by:
AndyAelbrecht earned 2000 total points
ID: 16391941
execute script thru ssh:

(from the php manual)

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$stream = ssh2_exec($connection, '/usr/local/bin/php -i');

fclose($stream);
fclose($connection);
?>

hope this helps,

Andy
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question