Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.