fopen and the working directory

I'm trying to open a text file using the fopen("file name"", "r"),  however I'm getting an error to the effect that the file can't be found.

On my local IIS server, where the fopen command works file, if I execute getcwd() it returns the current IIS directory. However, on my webserver thie returns c:\windows\system32\inetsrv. How can I force the PHP fopen to look in the same directory as the PHP script which is executing the fopen command?

Any help would be much appreciated

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
Typo ???

fopen("file name"", "r"),

2 "" ?

I would STRONGLY recommend that you use something like ...


$r_file = fopen('./filename', 'r');


Basically, tell the script WHICH directory.

. means THIS directory.

This is normally the directory that the main script is loaded from. If you are using fopen in an include file which is in a different directory to the main .php file, then the include directory is NOT the directory examined as . means the directory that the script is in.

If ANY code has changed the current directory beforehand, then you would need to know about this.

You could also go over the top and get the path for the .php script and use that ...


$r_file = fopen(dirname($_SERVER["SCRIPT_FILENAME"]) . DIRECTORY_SEPARATOR . 'filename', 'r');


This is probably the safest way to do it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.