tags:

views:

20

answers:

2

Hi,

Attempting to expand my knowledge by using PHP on the Command Line.

Currently I have a default installation of XAMPP, and have set up my Environment Variable.

I've been able to execute simple scripts like:

<?php echo 'hello world!'; ?>

Questions ~

Where do I store the scripts I am using? Currently I am doing:

C:\Users\Ross>php c:\helloworld.php

it works. Does this mean I need to specify a path every time? Or should I store php files inside my c:>xampp\php directory? I tried this and it doesn't appear to work.

What would be the accepted "best practice".

2nd question Could someone explain why this doesn't work:

    <?php  
    fwrite(STDOUT, "Enter file name:\n"); 

    $file=fgets(STDIN);
    print 'you entered...' . $file;

    $fp=fopen($file,'r');

        if(!$fp)
        { 
            print 'File could not be opened..'; 
        } 
        else 
        {
            /* show file pointer */
            print($fp);
        }
    ?>

and then I do:

C:\Users\Ross>php c:\file.php
Enter file name:
c:\foo.txt
you entered...c:\foo.txt

Warning: fopen(c:\foo.txt): failed to open stream: Invalid argument in C:\file.php on line 6
File could not be opened..

"foo.txt" is in the same directory and does exist.

thanks for any clarification.

+1  A: 

As far as were to store the files is concerned: I normally add the directory where php.exe is to my PATH environment variable, that way I can just call php in whatever directory contains the script I need to run. If you don't add the directory to PATH, then you would need to either run php from its directory and specify the full path to the PHP script, or run it from the directory where the PHP script is and specify the full path to the PHP executable.

Regarding opening the file: the reason this is occurring is because fgets is returning the newline from you pressing enter, too (it would seem). So in reality, it's trying to open a file whose name actually ends with a new line character.

Change the line: $file=fgets(STDIN);

to: $file=trim(fgets(STDIN));

and you should be fine.

Narcissus
great explanation, and that worked perfectlyso much to learn baha.thanks
Ross
A: 

question #1: all your php files should be inside the www folder of xampp (c:\xampp\www)

question #2: probably because you are not working in the correct folder.

xammp is good but I recommend you to use wamp, it's much easier to understand and use. Just google for it. xampp is more for those who are more techically skilled.

krike