views:

832

answers:

2

I know "something must have been changed" but my code seems to have broken over night for no reason.

My server directory structure is something like this:

/
/scripts
/audit
/other_things

I have a script (let's say it's called "/scripts/MyScript.php") in the "scripts" folder which gathers data from a webpage using curl, and saves a dated copy of the webpage it reads in the "audit" folder.

To write to the audit folder, I used

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

however that stopped working, 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

I have however fixed this by changing the path to

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

Logic dictates that something must have changed in the server configuration, but what? It is a dedicated server which I manage. How can I avoid something like this happening again?

I've even gone through SVN for MyScript.php and all previous versions have used the single . in the path.

A: 

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

That could have happened due to the path used to access the script, for example, if you did before http://website.co.uk/MyScript.php due to some url rewriting or whatnot and you are now accessing http://website.co.uk/scripts/MyScript.php.

I seem to recall there are other possible culprits but I can't remember them now. Did you mangle with some rewrite rules or URLs? (ie, started using PATH_INFO?)

Vinko Vrsalovic
+2  A: 

Use dirname(__FILE__) to get the filesystem path for the current file. Then use relative paths from there to locate your audit directory.

For example, within scripts/MyScript.php, dirname(__FILE__) will return /home/web/website.co.uk/scripts. You can reliably append /../audit to that.

(Note this even works in an included or required file—in that case it will return the directory in which the included file is located).

Nate