PHP Files Passing Input to a Program - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Files Passing Input to a Program - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Thursday, July 11, 2019

PHP Files Passing Input to a Program

PHP Files


Passing Input to a Program

Problem

You want to pass input to an external program run from inside a PHP script. For example, your database requires you to run an external program to index text and you want to pass text to that program.

Solution

Open a pipe to the program with popen(), write to the pipe with fputs() or fwrite(), and then close the pipe with pclose():

       $ph = popen('/usr/bin/indexer --category=dinner','w') or die($php_errormsg);
       if (-1 == fputs($ph,"red-cooked chicken\n"))        { die($php_errormsg); }
       if (-1 == fputs($ph,"chicken and dumplings\n")) { die($php_errormsg); }
       pclose($ph)                                                                  or die($php_errormsg);

Discussion

This example uses popen() to call the nsupdate command, which submits Dynamic DNS Update requests to name servers:

       $ph = popen('/usr/bin/nsupdate -k keyfile')                          or die($php_errormsg);
       if (-1 == fputs($ph,"update delete test.example.com A\n")) { die($php_errormsg);}
       if (-1 == fputs($ph,"update add test.example.com 5 A 192.168.1.1\n"))
                                                                                                                   { die($php_errormsg);}
       pclose($ph)                                                                                    or die($php_errormsg);

Two commands are sent to nsupdate via popen(). The first deletes the test.example.com A record, and the second adds a new A record for test.example.com with the address 192.168.1.1.

No comments:

Post a Comment

Post Top Ad