Deno Posted January 15, 2004 Posted January 15, 2004 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. Any help would be appreciated as always. Thanks. Quote
TCH-Don Posted January 15, 2004 Posted January 15, 2004 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 Quote
glassgorilla Posted January 15, 2004 Posted January 15, 2004 try changing this: >$fp = fopen( "notes/data/names.txt", "w" ); to this: >$fp = fopen( "./notes/data/names.txt", "w" ); Quote
Deno Posted January 16, 2004 Author Posted January 16, 2004 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? What's happening now? Quote
Deno Posted January 16, 2004 Author Posted January 16, 2004 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! Quote
Frylock Posted January 16, 2004 Posted January 16, 2004 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>"; } Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.