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.
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 include
d or require
d file—in that case it will return the directory in which the included file is located).