CLI ( command-line ) PHP displaying source code in-line when programis executed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Double Echo

    #1

    CLI ( command-line ) PHP displaying source code in-line when programis executed

    Hi all,

    I'm using PHP 4.4.2, and use PHP on both the command-line and the web.

    I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using
    Apache 2.0.55 , on my Dell laptop. Everything has been running flawlessly
    without problems. Very amazing to use VMware, it has worked beautifully.

    uname -a

    Linux xxxxxxx 2.6.13-15.8-default #1 Tue Feb 7 11:07:24 UTC 2006 i686 i686 i386
    GNU/Linux

    php -v
    PHP 4.4.2 (cli) (built: Feb 7 2006 20:13:29)
    Copyright (c) 1997-2006 The PHP Group
    Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

    php -m

    [PHP Modules]
    bcmath
    bz2
    calendar
    ctype
    curl
    dbx
    dio
    domxml
    exif
    ftp
    gd
    gettext
    gmp
    iconv
    mcrypt
    mssql
    mysql
    ncurses
    openssl
    overload
    pcre
    posix
    session
    shmop
    sockets
    standard
    sysvsem
    sysvshm
    tokenizer
    wddx
    xml
    yp
    zlib

    [Zend Modules]


    When I run the following program from the command line it prints out the
    source code as well as the output from the program. I run other PHP programs
    on the command-line and this doesn't happen. It's really peculiar. The
    error output is below.

    I copied this off of php.net, and have modified it only slightly to allow
    command-line input. I was trying to find a CSV solution, which I ended up
    finding elsewhere, but am curious why this program would error out the
    way it does--have I misconfigured something? My php.ini is basically
    unchanged, if at all. I think I might have set the path to mysql but that's
    about it. When I run this program from the web it works without error.

    <?php

    if ( $argv[0] )
    {
    $file = $argv[0] ;
    }
    else {
    $file = $_GET['file'] ;
    print "<font face=arial>\n" ;
    }

    $row = 1;

    $handle = fopen("$file", "r");

    while ( ( $data = fgetcsv($handle , 1000, ",") ) !== FALSE )
    {

    $num = count($data);

    print "$num fields in line $row:<br> \n";

    $row++;

    for ($c = 0; $c < $num; $c++)
    {
    print "$data[$c]<br> \n" ;
    }

    }

    fclose($handle) ;

    ?>


    === COMMAND-LINE OUTPUT ===


    1 fields in line 1:<br>
    <?php<br>
    1 fields in line 2:<br>
    <br>
    1 fields in line 3:<br>
    if ( $argv[0] )<br>
    1 fields in line 4:<br>
    {<br>
    1 fields in line 5:<br>
    $file = $argv[0] ;<br>
    1 fields in line 6:<br>
    }<br>
    1 fields in line 7:<br>
    else {<br>
    1 fields in line 8:<br>
    $file = $_GET['file'] ;<br>
    1 fields in line 9:<br>
    print "<font face=arial>\n" ;<br>
    1 fields in line 10:<br>
    }<br>
    1 fields in line 11:<br>
    <br>
    1 fields in line 12:<br>
    $row = 1;<br>
    1 fields in line 13:<br>
    <br>
    2 fields in line 14:<br>
    $handle = fopen("$file"<b r>
    r);<br>
    1 fields in line 15:<br>
    <br>
    3 fields in line 16:<br>
    while ( ( $data = fgetcsv($handle <br>
    1000<br>
    ,) ) !== FALSE ) <br>
    1 fields in line 17:<br>
    {<br>
    1 fields in line 18:<br>
    <br>
    1 fields in line 19:<br>
    $num = count($data);<b r>
    1 fields in line 20:<br>
    <br>
    1 fields in line 21:<br>
    print "$num fields in line $row:<br> \n";<br>
    1 fields in line 22:<br>
    <br>
    1 fields in line 23:<br>
    $row++;<br>
    1 fields in line 24:<br>
    <br>
    1 fields in line 25:<br>
    for ($c = 0; $c < $num; $c++) <br>
    1 fields in line 26:<br>
    {<br>
    1 fields in line 27:<br>
    print "$data[$c]<br> \n" ;<br>
    1 fields in line 28:<br>
    }<br>
    1 fields in line 29:<br>
    <br>
    1 fields in line 30:<br>
    }<br>
    1 fields in line 31:<br>
    <br>
    1 fields in line 32:<br>
    fclose($handle) ;<br>
    1 fields in line 33:<br>
    <br>
    1 fields in line 34:<br>
    ?> <br>




  • rlee0001

    #2
    Re: CLI ( command-line ) PHP displaying source code in-line when program is executed

    Echo Echo,

    At the beginning output $file. For some reason the file is reading
    itself as input. If you are using:

    php myprog.php infile.csv

    ....on the command line then I think you should be trying to get
    "infile.csv " (or whatever) using $argv[1] not $argv[0]. Otherwise you
    are passing the files own source to itself and the line

    print "$data[$c]<br> \n" ;

    Is causing the source to print.

    -Robert

    Comment

    • Double Echo

      #3
      Re: CLI ( command-line ) PHP displaying source code in-line whenprogram is executed

      You're right!!

      I'm smacking myself--too much coding!!

      I changed it on my other progs but forgot to on this one.

      Thanks!


      rlee0001 wrote:[color=blue]
      > Echo Echo,
      >
      > At the beginning output $file. For some reason the file is reading
      > itself as input. If you are using:
      >
      > php myprog.php infile.csv
      >
      > ...on the command line then I think you should be trying to get
      > "infile.csv " (or whatever) using $argv[1] not $argv[0]. Otherwise you
      > are passing the files own source to itself and the line
      >
      > print "$data[$c]<br> \n" ;
      >
      > Is causing the source to print.
      >
      > -Robert
      >[/color]

      Comment

      • Jasen Betts

        #4
        Re: CLI ( command-line ) PHP displaying source code in-line when program is executed

        On 2006-02-12, Double Echo <doubleecho@you r.com> wrote:[color=blue]
        > Hi all,
        >
        > I'm using PHP 4.4.2, and use PHP on both the command-line and the web.[/color]
        [color=blue]
        > When I run the following program from the command line it prints out the
        > source code as well as the output from the program. I run other PHP programs
        > on the command-line and this doesn't happen. It's really peculiar. The
        > error output is below.
        >
        >
        > 1 fields in line 1:<br>
        ><?php<br>
        > 1 fields in line 2:<br>
        ><br>
        > 1 fields in line 3:<br>
        > if ( $argv[0] )<br>
        > 1 fields in line 4:<br>
        > {<br>
        > 1 fields in line 5:<br>
        > $file = $argv[0] ;<br>[/color]

        .....



        [color=blue]
        ><?php
        >
        > if ( $argv[0] )
        > {
        > $file = $argv[0] ;
        > }
        > else {
        > $file = $_GET['file'] ;
        > print "<font face=arial>\n" ;
        > }[/color]

        argv[0] is the name of the PHP script.

        your program is reading itself and printing itself out.
        use argv[1];



        --

        Bye.
        Jasen

        Comment

        Working...