Jump to content

Recommended Posts

Posted

Hi,

 

I just starting learning about php reading text files, and I wrote a script, and then tested it on my server and I get this error message:

 

Warning: fopen(notes/data/names.txt) [function.fopen]: failed to create stream: No such file or directory in c:\inetpub\wwwroot\testwritepage.php on line 10

Couldn't open the data file. Try again later.

 

Here's my script:

 

<?php

$fp = fopen( "notes/data/names.txt", "w" );

 

if(!$fp)

{

echo "Couldn't open the data file. Try again later.";

exit;

}

?>

 

 

I don't understand why I'm getting errors already for such a simple lines of code that I copied and pasted from a tutorial site. :P Any help would be appreciated as always. Thanks. :)

Posted

While I am also learning,

questions..

$fp = fopen( "notes/data/names.txt", "w" );

 

are you trying to read the file? "r"

and does the folder notes exist below the current folder the test code is in?

and then data below that?

 

maybe try using a file in the same folder to start with.

 

But as i say, I am still learning

Posted

Hey! thanks for replying everyone!

 

You're right, I forgot to create the folder. Now that I did, I opened the page again on my web browser and now I get a blank page? B) What's happening now?

Posted

Nevermind...I got it to read the text file now after doing some reading and research. But I have one question though...

 

Say that on the text file, I have a list of words on there, so on my web browser, how do I get it to print one word from the text file, then next line, then the next word and so on and so on? cause right now, it's printing all the words on one line.

 

Any help would be appreciated, thanks again everyone! B)

Posted

I'm going to assume each word you want on a newline is on a newline in the text file... There might be better ways, but what I just do is read the entire file into an array, and then just go through the array and print out each element. Like...

 

>$file_array = file("/path/to/file.txt");
while (list($i, $val) = each($file_array)) {
  echo "$val<br>";
}

 

Or using your file pointer ($fp) try this.

 

>while (!feof($fp)) {
  $nextline = fgets($fp, 1024);
  echo $nextline . "<br>";
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...