Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15
  1. #1
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    <update> This problem has been fixed largely thanks to eKstreme. My last post in this thread sums up what worked for me. </update>

    Note: I only care about *nix (specifically, Linux) per this post.

    Locally there is a computer programming competition in which I competed last year, using C++. I don't really know C++; PHP is my thing. So I wrote the event coordinator, and to my delight, he said that he'd try and allow use of PHP along with BASIC/Python/Java/C/C++. However, he's not used to PHP.

    The program works like this:
    There is a piece of contest software, RockTest, that allows users to type their program in a text editor, and then automates compiling (if necessary). Then an input file with test data will be sent as standard input to the program; the output will be visible in the RockTest software (again, using standard output to pipe to RockTest, I believe).

    I'm not really familiar with Unix or Linux.

    How would one run a PHP file *while* sending it a file as input?

    Do you have to "chmod" the PHP file first?? I guess that could be set up in the RockTest software instead of actually compiling the PHP. But does it need done?

    Also, I assume we'd need a line like:
    Code:
    #!/usr/bin/php
    at the top?

    Any light you could shed would be much appreciated. Thanks.
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  2.  

  3. #2
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Here you go:

    http://us2.php.net/wrappers.php

    Good luck with the compo!
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  4. #3
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    I'm getting my test PHP file to run from bash using PuTTY.
    It even outputs:
    Code:
    u3573XXXX:~ > php -q stdin-stdout.php
    hello, world!
    But it doesn't seem to want to read from standard input.
    Code:
    (Command:)
    u3573XXXX:~ > php -q stdin-stdout.php < stdin-float.txt
    Code:
    (PHP:)
    <?php
    print fgets(STDIN) ."\n"; //should work--been around since v4.3.0
    ?>
    That doesn't output anything other than a blank line.

    How can I send a file to my PHP script as standard input, and read it?
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  5. #4
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Just had a play, this worked:
    Code:
    cat stdin-float.txt | php -q stdin-stdout.php
    Of course, fgets() returns up to the first newline unless you specify a length.

    Also, if you have info in a file, why don't you just read the file contents into a string and save yourself the hassle of STDIN?
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  6. #5
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Quote Originally Posted by eKstreme
    Code:
    cat stdin-float.txt | php -q stdin-stdout.php
    ...
    Also, if you have info in a file, why don't you just read the file contents into a string and save yourself the hassle of STDIN?
    Thanks :-) :-) :-) :-D I won't get to try that until tonight at the earliest.

    What's CAT? (Yeah I know, I'm a total *nix n00b) Does it just list file contents?

    As for your question, I'm not in charge of the environment: It will be just one of many languages supported by a certain programming competition. My job here was to figure out how to send a file to a php script as standard input (and ensure stdout worked too) before the coordinator could set up whatever he needs to do. It will be better, anyway, to have the input readily available as STDIN -- the test environment will make the "cat file | php scriptname" call automatically -- than force the contestants to open a file for input each time, IMO.

    Thank you VERY much!
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  7. #6
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    You're welcome.

    cat is short for concatenate, which does what it says on the tin: it reads the file and dumps it to stdout. This is useful, but is even more so because you can cat more than one file at the same time - hence the name.

    So a very simple merge of 3 files:

    Code:
    cat f1.txt f2.txt f3.txt > all.txt
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  8. #7
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Hah! Got it, cool.

    Unfortunatly, what you suggested didn't work for me.

    When you pipe input to php, it tries to run it, almost like code. Or you can do what I did:
    Code:
    php -q myfile.php
    Not both, as it says here: http://us2.php.net/features.commandline

    Sigh. What can I do?
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  9. #8
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Code:
    dir
    total 8
    -rw-r--r--    1 u3573657 ftpusers       13 Sep  1 22:22 input.txt
    -rwxr--r--    1 u3573657 ftpusers      119 Sep  1 22:22 multiply.php
    I added execute permissions to multiply.php, and tried this:
    Code:
    cat input.txt | multiply.php
    That gives this error message:
    Code:
    cat input.txt | multiply.php
    bash: ./multiply.php: No such file or directory
    I'm at a real loss here. It plainly exists, with execute properties thanks to chmod. and I did include this line in the PHP file:
    Code:
    #!/usr/bin/php -q
    What else can I possibly try??
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.

  10. #9
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Try:

    Code:
    cat input.txt | ./multiply.php
    And what did you mean by "When you pipe input to php, it tries to run it, almost like code."? Using cat, I managed to pipe a file's contents through to php, using the command I gave you...
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  11. #10
    Senior Member straight_up's Avatar
    Join Date
    Dec 2003
    Location
    Pennsylvania/Arizona
    Posts
    601
    Member #
    4309
    Quote Originally Posted by eKstreme
    Try:

    Code:
    cat input.txt | ./multiply.php
    And what did you mean by "When you pipe input to php, it tries to run it, almost like code."? Using cat, I managed to pipe a file's contents through to php, using the command I gave you...
    This should run script.php as PHP:
    Code:
    cat script.php | php -q
    Using similar methods (not cat) you can dynamically generate PHP code and run it.

    The more traditional way is:
    Code:
    php script.php
    The link I provided explains this, and says both methods can't be used together. Now, I understand and you understand we want to RUN script.php and INPUT input.txt when called like:
    Code:
    cat input.txt | php script.php
    but PHP probably doesn't.

    Did you say it worked for you, this old way?

    I'll try the new method -- actually, I have, though without the probably unnecessary "./" -- but I think the #! directive does not properly point to PHP! I'm not sure where 1&1 actually has their PHP... I'll call their tech support. Unless you can tell me how to determine where "php" actually points?

    I'll post again really soon...
    I am Alan Hogan (@alanhogan on Twitter). I like PHP, UI/UX design, and OS X.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:03 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com