Solved

PHP question on file paths in Windows OS

Posted on 2014-09-30
22
142 Views
Last Modified: 2014-10-05
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?
0
Comment
Question by:elepil
  • 8
  • 8
  • 5
22 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40353042
The man page is here: http://php.net/manual/en/function.include.php

I use Linux and not Windows, but the principles should be the same.  Note that I use the DIRECTORY_SEPARATOR constant because it's OS-aware and built into PHP.

My "home page" for the exercise is http://iconoun.com/demo/

Inside the "home page," I have this script.
<?php
error_reporting(E_ALL);

$new_path
= 'photos'
. DIRECTORY_SEPARATOR
. 'includes'
. DIRECTORY_SEPARATOR
. 'config.php'
;
require_once($new_path);

Open in new window

My directory tree looks like this:
demo/
|--index.php
|--photos/
   |--includes/
      |--config.php

Open in new window

Inside demo/photos/includes/config.php I have this script:
<?php echo 'HELLO FROM config.php.  Here is the getcwd(): ' . getcwd();

Open in new window

It all seems to work as planned, so unless there is some abstraction or something else hiding in your design, all I can say is, "try it this way."
0
 

Author Comment

by:elepil
ID: 40353158
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40353204
@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
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40356650
You can try this:
require_once(dirname(__FILE__) . "/includes/config.php");

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40358278
@taoyipai: That would need the "photos" directory in the path before it could work.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40358393
@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.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40358406
@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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40358456
@taoyipai: Yep!  It's not nearly as hard as it seems sometimes - you just gotta follow the rules.
0
 

Author Comment

by:elepil
ID: 40362226
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40362227
@elepil:  I believe the correct answer is here:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28528399.html#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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:elepil
ID: 40362235
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.
0
 

Author Comment

by:elepil
ID: 40362237
Also, E-E should provide a way for users to edit their post.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40362239
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.
0
 

Author Comment

by:elepil
ID: 40362248
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40362304
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.
0
 

Author Comment

by:elepil
ID: 40362370
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40362575
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.
0
 

Author Comment

by:elepil
ID: 40362584
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.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40362903
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 http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28528399.html#a40356650 a quick one?  Doesn't it work?
0
 

Author Comment

by:elepil
ID: 40362975
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.
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40363027
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Time difference 10 35
Phone Dialer 5 37
wordpress issue 2 22
selecting date modified field from a table 2 0
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now