Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

PHP question on file paths in Windows OS

I have XAMPP installed on a Windows 7 machine.

My application called "photos" resides in C:\xampp\htdocs\photos which has an "includes" subdirectory (i.e., C:\xampp\htdocs\photos\includes). In this "includes" directory is one file called config.php, and it is really still an empty file.

In my index.jsp, I have only one line:

<?php

require_once("\\photos\\includes\\config.php");

?>

Open in new window


When I run my index.jsp on a browser, I immediately get this warning and error message:

Warning: require_once(\photos\includes\config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\photos\index.php on line 3

Fatal error: require_once(): Failed opening required '\photos\includes\config.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\photos\index.php on line 3


But the file "\photos\includes\config.php" exists. Can someone please tell me why I'm getting this error?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

Ray, I used the DIRECTORY_SEPARATOR in my original example. I'm really getting frustrated now because I do one thing and it doesn't work, so I try to simplify it, and now I'm being told to do what I originally did which didn't work.

I believe this is probably a Windows issue, that's why I was very specific in mentioning I have Windows 7. I have seen this done in unix/Linux machines, and it worked fine.
@elepil: This will be my last on it.  I'm not telling you what to do.  I am showing what works.  The demonstration duplicates your directory structure as I understand it, and shows the code that works with include() and require().  I'll leave the demonstration scripts in place for a while longer so you can click the links and see it in action.
http://iconoun.com/demo/
http://iconoun.com/demo/photos/
http://iconoun.com/demo/photos/includes/
http://iconoun.com/demo/photos/includes/config.php

See also these notes on the man page.  They are the only ones that seem to address Windows/Linux differences:
http://us3.php.net/manual/en/function.include.php#94392
http://us3.php.net/manual/en/function.include.php#115360
Avatar of Brian Tao
You can try this:
require_once(dirname(__FILE__) . "/includes/config.php");

Open in new window

@taoyipai: That would need the "photos" directory in the path before it could work.
@Ray: I don't think so.  It returns the full path so the "photos" directory doesn't have to be in the path.
You may just try to
echo dirname(__FILE__);

Open in new window

and see the result.
@Ray: sorry I mistakenly thought that you were referring to "include_path".  I know what you meant, but we can actually know that "photos" IS in the path because the FATAL ERROR message which the author posted said it.
@taoyipai: Yep!  It's not nearly as hard as it seems sometimes - you just gotta follow the rules.
Avatar of elepil

ASKER

I've requested that this question be deleted for the following reason:

I really dislike this policy of Experts-Exchange when I do not give an A grade. You provide A, B, and C, then harass me when I give B or C. My time is valuable, and I resent being "contested" on this issue; this is not the first time in the years I've been a subscriber.

I did not give an 'A' because the solution wasn't really thorough, and yet, did help me. More specifically, Ray Paseur, through one of his links, confirmed to me that there is indeed a definite difference between Windows' PHP Xampp and Unix's Xampp. But he chose a lazy way of helping me, by providing me a link so I could read voluminous text instead of providing me a very specific fix for the problem. I spend extra time making my question concise and clear, and the kind of response I do not like is when someone provides a link and expects me to read through an entire lengthy article that may or may not address my very specific problem.

And as for your establishment, why do you challenge non-A ratings? If you insist on A-ratings, then don't provide the 'B' and 'C'. It is easier to just delete the question if I'm going to be bugged by you people over this.
@elepil:  I believe the correct answer is here:
https://www.experts-exchange.com/questions/28528399/PHP-question-on-file-paths-in-Windows-OS.html?anchorAnswerId=40353042#a40353042

Did you test that solution?  If you did, and it did not work exactly as posted, please show us the link to the URL that illustrates the failure, thanks.  You can get very good answers at E-E, but only if you work with the experts.
Avatar of elepil

ASKER

The real answer to my problem is (i.e., the answer that would've gotten an 'A'):

Unix and Windows does not handle PHP the same way. Given what I was trying to do:

\\photos\\includes\\config.php

Windows somehow interprets that to start from the C:\, hence it sees it as c:\photos\includes\config.php, but Unix does interpret it correctly by prepending it with the path leading up to the web server application root.

There was someone who mentioned this, but he presented it in a very uncertain fashion when he said "I think".

Nevertheless, I did give credit for this ticket, but I do not know why E-E is insisting I give it an 'A' rating when I don't feel it deserving. 'B' is not exactly bad. Now I have spent more time than I want on this, all because of the way E-E's internal preferences.
Avatar of elepil

ASKER

Also, E-E should provide a way for users to edit their post.
My question was, "Did you test the solution I posted?"  I know you said you did it differently with slashes and things in a quoted literal string, but that is contrary to what I recommended.  I just want to know if you copied the scripts, installed them and found that they failed.  If so, I want to understand why they failed, because I've been depending on this design pattern for many years.  If my work is at risk because of something I don't understand, I would sure like to know.  And that is why I'm asking for the URL of the demonstration case.  Thanks.
Avatar of elepil

ASKER

Ray, you clearly are a person who appreciates the value of time. I am the same way. When I get links from responders such as yourself, I click it and check the length right away. If I feel the length of the page is too long, I just skim over it and do not glean over it in excruciating detail. So the direct answer to your question, I did not try the script because that would've taken more time than I think I should devote to this problem. And I did eventually find the solution to my problem, thanks to one of the links you provided, but I don't think I needed to run any scripts.

Don't get me wrong, I am cognizant that some kinds of problems will deserve my time to read through a very lengthy article, but this one just isn't one of them. So when "experts" in E-E give me links, I frown on links that lead to solutions that are way too lengthy and may not even address my problem. This distaste I have for this kind of practice was borne out of past experience as I've wasted an inordinate amount of time just because the "expert" didn't feel like making me a brief code snippet or pithy explanation. I invariably conclude that "expert" as lazy.
So the direct answer to your question, I did not try the script because that would've taken more time than I think I should devote to this problem.
Wow, I wrote the script and tested it, to be sure it worked before I posted it.  But you did not try it because it would take too much of your time?  Sorry, but I just don't get it.  Anyway, good luck with your project.
Avatar of elepil

ASKER

Ray, when all has been said and done, I did give you credit. But when you told me you were running on a Unix system and not Windows, I felt any scripts you wrote will have limited applicability. As I've always said, I do appreciate your efforts, but perhaps exercising a little empathy towards the person asking the question might guide you to the most efficacious response.
Like I said, good luck with the project.  I don't mean to be un-empathetic, but really, these are just simple technical questions.  It's about what has been tested and shown to work.  I would not waste your time with speculation (unless I warned that I was guessing) when a tested and working example can be given.  I kind of feel like that's a minimum obligation to participation in an online forum like this one.
Avatar of elepil

ASKER

Ray, when a user like me posts a problem, I'm trying to find a quick solution. My question is not something like "How do I upload files in PHP?" Had my question been that complex, then I would've run whatever script you made.

But my question was a simple path problem. I never fathomed I'd end up being told to run scripts for a question like this. I was expecting a very quick answer. I should also add that just as you, a responder, expects the user to be concise and to-the-point with his question, I also expect responders to be as concise and to-the-point with their responses. It is my prerogative to decide whether a response given to me is pertinent or convoluted/excessive. You really should not take this personally.
Wow! This is interesting.
when a user like me posts a problem, I'm trying to find a quick solution.
Then isn't the solution:
require_once(dirname(__FILE__) . "/includes/config.php");

Open in new window

in my comment https://www.experts-exchange.com/questions/28528399/PHP-question-on-file-paths-in-Windows-OS.html?anchorAnswerId=40356650#a40356650 a quick one?  Doesn't it work?
Avatar of elepil

ASKER

Taoyipai, when I first saw your post, I noticed Ray Paseur disputing with you on your response. Whenever I see responders seemingly debating with each other about their solutions, my first reaction is not to take it seriously. I know that probably doesn't sound right, but that's how I ended up reacting.

I'm new to PHP, so even the dirname() function was new to me. I later somehow ended up reading about dirname(), and after playing around with it, decided it was the best solution. But I now realize that you had posted about this before I even discovered it, and you really deserved credit for it. I wish I could go back and redistribute the points, but I don't think E-E's system allows me to do that. I screwed up when I had inadvertently overlooked your response and did not give you credit when I closed the ticket, and for that I apologize.

But I will remember your name. Being new to PHP, expect me to ask a lot more questions, and I hope you will still be kind enough to respond to me again. I can only promise to do my best never to overlook any responses again in the future.
Well, I didn't really answer your question:
Can someone please tell me why I'm getting this error?
And I was just giving you something that should work.  Anyway, I'm glad to know that it's working now.