By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

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

P: n/a
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"<br>
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);<br>
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>


Feb 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Feb 12 '06 #2

P: n/a
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:
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


Feb 12 '06 #3

P: n/a
On 2006-02-12, Double Echo <do********@your.com> wrote:
Hi all,

I'm using PHP 4.4.2, and use PHP on both the command-line and the web. 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>
.....

<?php

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


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

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

--

Bye.
Jasen
Feb 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.