Php fopen relative path broken - mystery

I know "something had to be changed", but my code seems to have broken at night for no reason.

My server-to-server structure looks something like this:

/  
/ scripts  
/ audit  
/ other_things

I have a script (let's say it's called "/scripts/MyScript.php") in the "scripts" folder that collects data from a web page using curl and saves a dated copy of the web page that it reads into the "audit" folder ...

To write to the audit folder, I used

$ fh = fopen ("./audit/2008-09-09-183000.backup.log", "w");

however, what stopped working by throwing

[function.fopen]: Failed to open stream: No such file or directory in /home/web/website.co.uk/audit/2008-09-09-183000.backup.log on line 353

However, I fixed this by changing the path to

"../audit/2008, etc." from "./audit/2008" (two full stops / periods instead of one)

Logic dictates that something should have changed in the server configuration, but what? This is a dedicated server that I manage. How can I avoid repeating something like this?

I even went through SVN for MyScript.php and all previous versions used single. on my way.

+2


source to share


2 answers


Use dirname(__FILE__)

to get the file system path for the current file. Then use relative paths from there to find the directory audit

.

For example, within scripts/MyScript.php

, dirname(__FILE__)

will return /home/web/website.co.uk/scripts

. You can reliably add /../audit

to this.



(Note that this even works on file include

d or require

d - in which case it will return the directory where the included file is located).

+7


source


Your CWD (current working directory) has changed, it was the document root, and now it is root / scripts.

This could be due to the path used to access the script, for example if you did this before http://website.co.uk/MyScript.php due to some url rewriting or whatever, and now you access http://website.co.uk/scripts/MyScript.php .



I seem to remember there are other possible criminals, but I can't remember them. Have you fought with some rewriting rules or URLs? (i.e. started using PATH_INFO?)

0


source







All Articles