Jump to content

Fopen() Question Concerning Relative Path (Mac With Xampp)


Xingyiman

Recommended Posts

So, I'm starting the book "PHP and MySQL Web Development 4th Edition" and have a question about some code from chapter 2.

 

In the sample script you declare a variable

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

 

This is then used later when opening/creating a file for writing:

 

@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

 

Unfortunately, it gives me an error saying the directory doesn't exist.

 

Now if I eliminate the /.. part of the path it works fine.

 

I seem to be having a reading comprehension problem as the text says ".." is used to mean "the parent directory of the document root directory" and I'm not entire sure what this means. Right now my orders folder is in the same folder as my script that executes this command:

 

/Applications/XAMPP/xamppfiles/htdocs/processorder.php

I seem to be having a reading comprehension problem as the text says ".." is used to mean "the parent directory of the document root directory" and I'm not entire sure what this means. Right now my orders folder is in the same folder as my script that executes this command:

 

/Applications/XAMPP/xamppfiles/htdocs/processorder.php

 

Given that information, then most likely your $DOCUMENT_ROOT is defined as:

/Applications/XAMPP/xamppfiles/htdocs/ This is the folder the web server treats as the root when resolving URL paths, eg http://localhost/blah would resolve to /Applications/XAMPP/xamppfiles/htdocs/blah

 

Now, the parent of that would then be /Applications/XAMPP/xamppfiles which is where the .. takes you to.  Then you add on the rest of the path information to end up with a final path of:

/Applications/XAMPP/xamppfiles/orders/orders.txt

 

That is where it is attempting to find the file at.  If the directory /Applications/XAMPP/xamppfiles/orders does not exist, you will need to create it before running the script.

Thanks! I see what the book was saying now. It mentioned security, so using the $_SERVER['DOCUMENT'] variable to get the root directory and putting the file in a folder one directory above the root (the parent directory of the root) takes the potentially sensitive information out of the main tree of the website and accesses it with a relative path, which makes it more secure than using a direct path in the site tree.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.