This is a discussion on fclose within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello, I have these lines: $fp = "test.txt"; if ($datei = fopen ($fp, "r")) { while (!feof ($datei)) { $...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
I have these lines: $fp = "test.txt"; if ($datei = fopen ($fp, "r")) { while (!feof ($datei)) { $zeichen = fgetc ($datei); echo ($zeichen); } fclose($fp); } The file is opened correctly and the content of the file is shown. But below the content there is the error: Warning: fclose(): supplied argument is not a valid stream resource in ........... on line 9 Where is the mistake? Thanks Werner |
|
|||
|
*** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200):
> $fp = "test.txt"; > if ($datei = fopen ($fp, "r")) { [...] > fclose($fp); > The file is opened correctly and the content of the file is shown. But > below the content there is the error: > Warning: fclose(): supplied argument is not a valid stream resource in > .......... on line 9 > Where is the mistake? The error is that $fp is not a valid stream resource. Actually, $fp is a string. Let's check the manual: Usage: resource fopen ( string filename, string mode [, int use_include_path [, resource zcontext]] ) Purpose: Opens file or URL Usage: bool fclose ( resource handle ) Purpose: Closes an open file pointer So fclose() doesn't expect a string as an argument. Can you see now where to get a resouce handle from? -- -- Álvaro G. Vicario - Burgos, Spain -- Thank you for not e-mailing me your questions -- |
|
|||
|
Hello,
"Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> schrieb im Newsbeitrag news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net... > *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200): >> $fp = "test.txt"; >> if ($datei = fopen ($fp, "r")) { > [...] >> fclose($fp); > >> The file is opened correctly and the content of the file is shown. >> But >> below the content there is the error: >> Warning: fclose(): supplied argument is not a valid stream resource >> in >> .......... on line 9 >> Where is the mistake? > > The error is that $fp is not a valid stream resource. Actually, $fp > is a string. Let's check the manual: > > Usage: resource fopen ( string filename, string mode [, int > use_include_path [, resource zcontext]] ) > Purpose: Opens file or URL > > Usage: bool fclose ( resource handle ) > Purpose: Closes an open file pointer > > So fclose() doesn't expect a string as an argument. Can you see now > where to get a resouce handle from? > first: I'm a beginner so I have taken the example out of a book and there it was written exactly like above. Now I have modified it, like you said and it works. Thanks, Werner |
|
|||
|
*** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 14:05:02 +0200):
> first: I'm a beginner so I have taken the example out of a book and > there it was written exactly like above. For some reason all programming books have typos in basic examples :) -- -- Álvaro G. Vicario - Burgos, Spain -- Thank you for not e-mailing me your questions -- |
|
|||
|
of course, this does'nt work because fclose wait the handle of the file open
with fopen ! if ($datei = fopen ($fp, "r")) this is wrong..... $datei is the handle of the open file... $handle=fopen ($fp, "r"); ....... work on file fclose($handle); yann. "Neudeck Werner" <w.neudeck@web.de> a écrit dans le message de news: clqnh5$1b5$1@ngspool-d02.news.aol.com... > Hello, > > "Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> schrieb > im Newsbeitrag news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net... >> *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200): >>> $fp = "test.txt"; >>> if ($datei = fopen ($fp, "r")) { >> [...] >>> fclose($fp); >> >>> The file is opened correctly and the content of the file is shown. But >>> below the content there is the error: >>> Warning: fclose(): supplied argument is not a valid stream resource in >>> .......... on line 9 >>> Where is the mistake? >> >> The error is that $fp is not a valid stream resource. Actually, $fp is a >> string. Let's check the manual: >> >> Usage: resource fopen ( string filename, string mode [, int >> use_include_path [, resource zcontext]] ) >> Purpose: Opens file or URL >> >> Usage: bool fclose ( resource handle ) >> Purpose: Closes an open file pointer >> >> So fclose() doesn't expect a string as an argument. Can you see now where >> to get a resouce handle from? >> > first: I'm a beginner so I have taken the example out of a book and there > it was written exactly like above. > Now I have modified it, like you said and it works. > Thanks, Werner |
|
|||
|
Hello yann,
"yann kloniecki" <yklo@club-internet.fr> schrieb im Newsbeitrag news:41811cec$0$15755$7a628cd7@news.club-internet.fr... > of course, this does'nt work because fclose wait the handle of the > file open with fopen ! > if ($datei = fopen ($fp, "r")) this is wrong..... > $datei is the handle of the open file... > $handle=fopen ($fp, "r"); > ...... work on file > fclose($handle); > > yann. > > > "Neudeck Werner" <w.neudeck@web.de> a écrit dans le message de news: > clqnh5$1b5$1@ngspool-d02.news.aol.com... >> Hello, ........... >> "Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> >> schrieb im Newsbeitrag >> news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net... >>> *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 >>> +0200): ........... in the meantime I have understood it completeley, thanks to you and Alvaro. After reading your postings and studying the manual, everything works. I only wonder, why there is such an example in a book for beginners. And in the meantime I have realized, that it's not the only mistake in this book (the name ist "PHP4 for dummies") Thanks to all Werner |