Xingyiman Posted December 27, 2012 Share Posted December 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272391-fopen-question-concerning-relative-path-mac-with-xampp/ Share on other sites More sharing options...
kicken Posted December 27, 2012 Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272391-fopen-question-concerning-relative-path-mac-with-xampp/#findComment-1401433 Share on other sites More sharing options...
Xingyiman Posted December 27, 2012 Author Share Posted December 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/272391-fopen-question-concerning-relative-path-mac-with-xampp/#findComment-1401439 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.