fopen failed to open stream

Problems with the Windows version of XAMPP, questions, comments, and anything related.

fopen failed to open stream

Postby PaulSharpe » 02. December 2008 21:12

I am testing on my localhost. The tests for http://localhost and http://localhost/index.html work fine, as well as a phpinfo() html module.
I am running Windows VISTA (64).

But, when I try to use the fopen, I get the "Warning: fopen (C:/xampp/htdocs/../Files/order.txt) [function_fopen]: failed to open stream. No such file or directory in C:\xampp\htdocs\Source\ProcessOrder.php on line 12."

Any thoughts on what I have missing?
Thank you.
Last edited by PaulSharpe on 02. December 2008 23:46, edited 1 time in total.
PaulSharpe
 
Posts: 5
Joined: 29. November 2008 23:18

Postby Nobbie » 02. December 2008 22:34

Simply read the error message carefully:

>C:/xampp/htdocs/..Files/order.txt


Do you think, this is a valid pathname?
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04

Postby PaulSharpe » 02. December 2008 23:33

Thank you for the observation on the file name, I had to type it into the forum because for some reason I can't do a copy and paste.

The file name from the "Warning: fopen" message is C:/xampp/htdocs/../Files/orders.txt.

Other thoughts?
Thank you.
PaulSharpe
 
Posts: 5
Joined: 29. November 2008 23:18

Postby glitzi85 » 03. December 2008 02:38

PaulSharpe wrote:The file name from the "Warning: fopen" message is C:/xampp/htdocs/../Files/orders.txt.

Other thoughts?

Does the file C:\xampp\Files\orders.txt exist?

glitzi
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

Postby PaulSharpe » 03. December 2008 03:32

???Does the file C:\xampp\Files\orders.txt exist?

Yes, to test it, I copied and pasted the file name from the error msg into the url - http://localhost/Files/order.txt and it opened up the text file. So I think I can say that the file exists and is accessible through the localhost.

Thank you for asking - other thoughts?
PaulSharpe
 
Posts: 5
Joined: 29. November 2008 23:18

Postby glitzi85 » 03. December 2008 05:01

In your PHP Code you are requesting C:\xampp\htdocs\..\Files\orders.txt. That is equal to C:\xampp\Files\orders.txt. If you request http://localhost/Files/orders.txt, then you would request the File C:\xampp\htdocs\Files\orders.txt

Compare:

Code: Select all
Web: C:\xampp\htdocs\Files\orders.txt  (=http://localhost/Files/orders.txt)
PHP: C:\xampp\Files\orders.txt         (=C:\xampp\htdocs\..\Files\orders.txt)


Repair your PHP Script.

Here some stuff you should read:

http://en.wikipedia.org/wiki/Relative_path
http://httpd.apache.org/docs/2.2/mod/co ... cumentroot

glitzi
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

Re: fopen failed to open stream

Postby PaulSharpe » 03. December 2008 16:31

glitz1: Thanks for trying. I reviewed the info on file paths but didn't learn anything new. But I think I have the problem identified. Now I need to find out how to solve it.
This is for Windows VISTA.

If I don't put "orders.txt" in a sub-root folder, php works fine. As soon as I put my "orders.txt" in a "files" folder, php can't find it.

Test cases:
1. I put the "orders.txt" file in the root directory and php works fine:
system: C:\xampp\htdocs\orders.txt
php:
Code: Select all
  $DOCUMENT_ROOT = $_SERVER ['DOCUMENT_ROOT'] ;
  $fp = fopen ("$DOCUMENT_ROOT/../orderts.txt", 'w') ;
  echo "DOCUMENT_ROOT string: " . $DOCUMENT_ROOT ;
  echo "File open string: " . $fp


http://localhost/ProcessOrder.php resuts:
DOCUMENT_ROOT string: C:/xampp/htdocs
File open string: Resource id #3

2. I put the "orders.txt" file in a "files" folder in the root directory and php can't find it:
system: C:\xampp\htdocs\files\orders.txt

php:
Code: Select all
  $DOCUMENT_ROOT = $_SERVER ['DOCUMENT_ROOT'] ;
  $fp = fopen ("$DOCUMENT_ROOT/../files/orderts.txt", 'w') ;
  echo "DOCUMENT_ROOT string: " . $DOCUMENT_ROOT ;
  echo "File open string: " . $fp ;


http://localhost/ProcessOrder.php resuts:
DOCUMENT_ROOT string: C:/xampp/htdocs
File open string:
Warning: fopen(C:/xampp/htdocs/../files/orders.txt) [function.fopen]: failed to open stream: No such file or directory in C:\xampp\htdocs\ProcessOrder.php on line 14

Am I required to put all of my files in the root directory?

Thank you.
PaulSharpe
 
Posts: 5
Joined: 29. November 2008 23:18

Re: fopen failed to open stream

Postby glitzi85 » 03. December 2008 17:29

OK, you don't understand it. Maybe you learn it in this way:

Start -> run -> cmd.exe

Now you have the command line open type this command:

Code: Select all
c:
cd \


Now the Command will look like this:

Code: Select all
C:\>


Now you follow your Path from the code, type:

Code: Select all
cd C:\xampp\htdocs\..\


Now you will not be in the folder htdocs, you will be in the folder xampp. And THERE(!) the orders.txt will be created!

In your second code you try to create the file in the subdirectory files, try it in the Console:

Code: Select all
cd C:\xampp\htdocs\..\files\


You will get an error, saying the system can not find the path. Thats correct, because you want to open C:\xampp\files, and this folder is not existing. PHP can only create files with the fopen command, directorys are not created. That's the reason why the second code is not working!

Your problem all the way through this post are just the two dots:

Code: Select all
$fp = fopen ("$DOCUMENT_ROOT/../files/orderts.txt", 'w') ;


Replace it with this code:

Code: Select all
$fp = fopen ("$DOCUMENT_ROOT/files/orderts.txt", 'w') ;


and everything should be fine. But it is very important that you understand why it is working now, otherwise you will have problems all the time.

glitzi
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

Re: fopen failed to open stream

Postby PaulSharpe » 03. December 2008 19:37

You have been very patient!! Thank you.

I am such a dumkoff.

I have used the "../" to go up the hierarchy before, but I have never seen it used in the middle of a directory tree. I thought it had some magic stuff to do with the Apache.

I understand it now, and my "files" folder works fine.

Thanks a bunch.
Have a good day.
PaulSharpe
 
Posts: 5
Joined: 29. November 2008 23:18


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 52 guests