Question

I have a folder \folder\ above the webroot that contains .php, .inc, .dat files

  • the .php can access the .inc no problem

  • but when the .inc tries to access the .dat using fopen('mydat.dat', "rb"); it gives an error that it can't find mydat.dat inside \folder\myinc.inc

Of course it can't find it since .inc is a file not a folder. Why is php treating it as such?

Any ideas why php is trying to find the .dat inside the .inc?

or any other alternatives to fopen($filename, "rb")?

Was it helpful?

Solution

After reading your comments, I think you expect fopen to use the include_path.

fopen() doesn't use the include_path by default(unlike include). It's an option. See the manual. http://www.php.net/manual/en/function.fopen.php

OTHER TIPS

Learn filesystem basics. your working directory is still in the webroot. it doesnt change it to the \folder\ (although it seems new versions of PHP do look files within current file location).

Anyway if you want to open a file in the same directory, dirname(__FILE__) is always for you

in the myinc.inc you can use

fopen(dirname(__FILE__).'/mydat.dat', "rb");

Sounds like you have safe_mode enabled, but don't have a value for open_basedir, or have the file outside it. Put the .dat file in a path given to that option.

When ever we deal with files in php. It is necessary to give path+file name to avoid such type of errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top