0

How come my .php fopen() methods are not working. It says that it can't find the file? Any help is greatly appreciated:

 #!/usr/local/bin/php <?php $strLessonDescription = fopen("c:\\exercise5\\content\\lesson5.txt", "r") or die ("Error - lesson5.txt cannot be opened"); $arrLessonVocabulary = fopen("c:\\lovej2ee\\exercise5\\content\\vocabulary5.txt", "r") or die ("Error - lesson5.txt cannot be opened"); ?> 
1
  • Please don't edit questions and completely change the question. If you have new data or want to make fixes that's fine, but you changed this to be an entirely different question. Now the answers aren't relevant to the question, voting on relevant answers is meaningless, and they can't be archived for other users. If you have a new question, open a new question. Commented Apr 14, 2011 at 20:30

2 Answers 2

1

Guessing you're on windows given the path format: why are you not specifying the driver letter? e.g.:

 "c:\\dir\\dir\\thing.txt" 

Starting with "\\dir" gives you an absolute path on the "current drive", whatever it happens to be in the context of what runs that PHP code. If you want a normal relative path, drop the first \\.

If you want a UNC path, that would be:

"\\\\host\\share\\dir\\..." 

(If you're on a unix-type plateform, well, the slashes are the wrong way around, but the relative vs absolute path thing remains.)

[And your two paths are suspiciously inconsistent. But no idea if that is intended or not.]

3
  • Okay so I got it to work -- almost. See the above revised code. The only problem I am having now is with my fread (). For some reason it refuses to acknowledge my file path even though the "fopen()" and "file()" methods already have. Looking at the http path provided for $strLessonDescription what path will the "filesize()" method accept? Commented Apr 14, 2011 at 17:39
  • you've completely changed the question. you shouldn't do that, the answers posted make no sens anymore. Please revert your edit and post a new question. Commented Apr 14, 2011 at 17:47
  • Sorry dude. I just changed to code back. . ish. I will post a new question. As soon as I get enough points I will vote up your answers. Thanks for helping me! Commented Apr 14, 2011 at 18:01
0

I agree with Mat that you may want to be more explicit about your intent of the file path. Also, I further suggest that may want to ensure the file exists before attempting to open it. You can do this with file_exists():

$filename = '\exercise5\content\lesson5.txt'; if (file_exists($filename)) { //Your code here } else { //error gracefully } 

If you can update your original question with more information, the help you receive may be more poignant.

EDIT: Per your updated question (which is a different question altogether), the fact that the file you're trying to read is actually via a remote request, rather than local file, changes things significantly. Per the fread() manual page, you need to read the file differently. Alternatively, simply use file_get_contents().

2
  • Okay so I got it to work -- almost. See the above revised code. The only problem I am having now is with my fread (). For some reason it refuses to acknowledge my file path even though the "fopen()" and "file()" methods already have. Looking at the http path provided for $strLessonDescription what path will the "filesize()" method accept? – Commented Apr 14, 2011 at 17:41
  • Nevermind. I will follow Mat's advice and start a new question. Commented Apr 14, 2011 at 18:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.