Program execution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atlkhan
    New Member
    • Apr 2007
    • 39

    Program execution

    I am using php to write inputs I receive from browser into a text file.Than I use shell_exec() to execute a program on server that writes those inputs from the text file and write its output to another text file. Now after my program has written its output I want to read this text file from php and send it back to the browser.
    Can someone please guide me how I can do this.


    Thanks
  • shoonya
    New Member
    • May 2007
    • 160

    #2
    if you know the path of the file then use
    [PHP]fopen [/PHP]
    to open the file with required mode like
    [PHP]a - append
    r - read
    w - write[/PHP]
    etc
    fopen will return a handle to the opened file
    then use [PHP]fget [/PHP]to retrieve the contents ad display it to client

    shoonya

    Comment

    • atlkhan
      New Member
      • Apr 2007
      • 39

      #3
      Thanks Shoonya,

      I think I didn't explain my problem clearly, issue is not reading the output file. Issue is how you know that file has been written by the program and read the valid data(i.e what to do in the php code while the program is running and writting to the file. And how to know that text file has been written)
      .



      Originally posted by shoonya
      if you know the path of the file then use
      [PHP]fopen [/PHP]
      to open the file with required mode like
      [PHP]a - append
      r - read
      w - write[/PHP]
      etc
      fopen will return a handle to the opened file
      then use [PHP]fget [/PHP]to retrieve the contents ad display it to client

      shoonya

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Originally posted by atlkhan
        I am using php to write inputs I receive from browser into a text file.Than I use shell_exec() to execute a program on server that writes those inputs from the text file and write its output to another text file. Now after my program has written its output I want to read this text file from php and send it back to the browser.
        Can someone please guide me how I can do this.


        Thanks
        I have done something similar using the following code:
        Code:
        $lccontent = file($lcfilename); // $lcfilename is a variable that contains the relative path to the file
        
        // put it into an array
        $ladownloads = explode("||", $lccontent[0]); // my file is formatted for my requirements. Check out the options for explode for further information
        // you can then loop through the array using foreach()
        Hope that helps

        Nathan

        Comment

        • shoonya
          New Member
          • May 2007
          • 160

          #5
          use a flag either in php code which will tell you that file has been written
          or put some special symbols at the last of the text files and try seeking fo that symbol using the file
          if it's not there then file has still some content to be written

          shoonya
          (i may sound like newbie but that's what i am)
          :D

          Comment

          • atlkhan
            New Member
            • Apr 2007
            • 39

            #6
            Thanks Nathan, can you please tell what you are trying to do here?


            Originally posted by nathj
            I have done something similar using the following code:
            Code:
            $lccontent = file($lcfilename); // $lcfilename is a variable that contains the relative path to the file
            
            // put it into an array
            $ladownloads = explode("||", $lccontent[0]); // my file is formatted for my requirements. Check out the options for explode for further information
            // you can then loop through the array using foreach()
            Hope that helps

            Nathan

            Comment

            • Purple
              Recognized Expert Contributor
              • May 2007
              • 404

              #7
              Hi,

              as an alternative approach, what getting the external program to output a message on completion and test this within your script:

              [PHP]$result = shell_exec('pin g -?');
              if ($result == 'ok') echo "file processed ok";
              else echo "oh no.. it failed !";[/PHP]

              Regards Purple

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by atlkhan
                Thanks Nathan, can you please tell what you are trying to do here?
                Sorry for the delay in getting back to you.

                What my code does is this:
                1) File() opens the file and puts it's contents into a variable
                2) In my specific file I had a number of main elements separated by '||', inside this I have a number of elements separated by '&&'. for example:

                Code:
                1&&downloads/pdf/monthlyprayerjuly2007.pdf&&Monthly Prayer Bulletin - July 2007||0&&downloads/pdf/monthlyprayerjune2007.pdf&&Monthly Prayer Bulletin - June 2007||3&&http://www.hope08.com/Media/AllMedia.aspx&&Hope '08 Promotional DVD (External Link)||0&&downloads/video/justtenpromo.wmv&&Just 10 Promotional DVD (Windows Media Player)||0&&downloads/video/justtenpromo.rmvb&&Just 10 Promotional DVD (Real Player)
                3) First of all I explode the file contents based on '||', this creates an array of elements such as:
                Code:
                1&&downloads/pdf/monthlyprayerjuly2007.pdf&&Monthly Prayer Bulletin - July 2007
                Then looping through this array I explode each element once more into an array based on '&&', this allows me to print the results to screen.

                For an example of this in operation take a look at this page this is the code in action. I used this functionality to replicate what I would achieve with a DB because the host for this mini project did not enable us to have a DB.

                Hope that makes sense, I know it's a bit twisted and devious but the concept is sound and I think may help you in what you are trying to achieve - at least I hope it will.

                Cheers
                nathj

                Comment

                Working...