PHP file handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mailtwogopal
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 12:29 am

PHP file handling

Post by mailtwogopal »

Hi all,

I am absolutely a beginner to PHP and even to developing. i am learning php via. http://www.phphelps.com and at one stage i came across File handling in php. Below here is the example code given in the tutorial

Code: Select all

 
    <?php
    $file = "data.txt";
    $fp = fopen($file, "r");
    while(!feof($fp)) {
    $data = fgets($fp, 1024);
    echo "$data <br>";
    }
    fclose($fp);
    ?>
 
When i run this code, i got into infinite loop and moreover i don't know where i have to store the file "data.txt" in my local hard drive. Even i tried with the full path to read this file but am getting error "No such file or directory exists". Presently i stored this file in the directory which is the web server's (Apache)\htdocs. can anyone please help with this? so that i can move further in my learning.

Thanks in Advance
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: PHP file handling

Post by mintedjo »

If you save the file in the same directory as the php file you will only need the filename "data.txt".
and what is this character? 皜 xD
mailtwogopal
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 12:29 am

Re: PHP file handling

Post by mailtwogopal »

mintedjo wrote:If you save the file in the same directory as the php file you will only need the filename "data.txt".
and what is this character? 皜 xD
Hi,

It has been removed and it seems some typographical mistake. even i tried the same using "foreach" loop it din't work. Yes, i tried using "data.txt" simply and i was getting same error. kindly help me.
mailtwogopal
Forum Newbie
Posts: 4
Joined: Mon Jan 19, 2009 12:29 am

Re: PHP file handling

Post by mailtwogopal »

Hi guys,

Atlast i found the error and corrected. it works fine now! i made a small mistake while saving a data.txt with .txt extension 2 times. anyway thanks.
Post Reply