?
Solved

PHP - file_exists problem

Posted on 2007-07-23
11
Medium Priority
?
2,195 Views
Last Modified: 2011-10-03
I'm having trouble with this PHP code.
The file DOES exist, but it's giving me the other result.

if(!file_exists($file))
            {
                        echo "Does not exist";
            }
            else
            {
                        echo "Yes! Exists!";
            }


I need help. Why is it working backwards?
Are there any other options?
0
Comment
Question by:innercproductions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 19552785
The web server needs read access to the file and the folder that contains it I believe. Try checking that.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 375 total points
ID: 19552792
Also, try outputting the value of $file and of getcwd() to check the filename and current folder is what you think it is.
0
 
LVL 13

Assisted Solution

by:MasonWolf
MasonWolf earned 375 total points
ID: 19552840
The function "file_exists" should work fine. Are you sure you're looking inside the directory where you think you are?

Also, what sort of file are you checking for? If you're running in safe mode, certain files relating to the operation of your server won't be accessible to you, and "file_exists" will return false.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 375 total points
ID: 19553753
What is the content of $file and where is that file located on disk? If $file a relative path or a full path?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 19554607
please post an example for $file
0
 

Author Comment

by:innercproductions
ID: 19560324
I think it's working now. But I'm not sure what I changed.

Question:  what does the PHP safe mode do? In simple terms... short description. AND... is it really needed?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 19560370
Note in the PHP manual (http://jp.php.net/manual/en/features.safe-mode.php):
"Safe Mode was removed in PHP 6.0.0"

Quote from: http://www.webhostgear.com/319.html

Safe mode is an easy way to lock down the security and functions you can use. PHP.net explains php safe_mode as, "The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren't very realistic, many people, especially ISP's, use safe mode for now."

I highly recommend you enable safe_mode on production servers, especially in shared environments. This will stop exec functions and others that can easily prevent a security breach.

0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 375 total points
ID: 19560426
> I think it's working now. But I'm not sure what I changed.
I guess it is/was a caching problem (see docs about file_exists() function)

> I highly recommend ..
.. not to use safe_mode=on except you have 101% understood what it really does.
It's far away from being an "easy security check"!
safe_mode is probably one of the most common mistakes where poeple believe that they are "safe" then.
You better go with your own parameter checking and reject all and every request which does not comply to your checks.
0
 

Author Comment

by:innercproductions
ID: 19565321
Thanks for the info.
Anyone want to suggest how to divide/give points?
0
 
LVL 13

Expert Comment

by:MasonWolf
ID: 19565466
Do you think your solution had anything to do with taking PHP out of safe mode? If so, then please pick my solution as the primary one (250) and offer assists (125 ea) to Terry and hoffman. If not, then just divide the points evenly between Terry and hoffman, because you asked a new question which they answered and I did not participate in.

That's my advice anyway. If you're primary question was never answered and your secondary was, then it seems fair to divide the points between those who answered the secondary question.
0
 

Author Comment

by:innercproductions
ID: 19565572
Since none of you hit the nail on the head (probably because I didn't know exactly what to ask)... but all of you did help me with info and somehow probably led me to the solution... I will divide points evenly.

I turned SafeMode back on and restarted the webserver... nothing changed. It still works.
So I don't think that was it MasonWolf. But i did consider your suggestion. Thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 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