问题
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.
回答1:
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).
回答2:
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?)
回答3:
Is too late for this answer sure, i am a learner maybe help someone else!
$dir=(__DIR__).'/'; //Path to current script location
$path="../"; //Use any relative path to your script as you want and exists
$file="file.txt"; //A dummy test file
$fullpath=$dir.$path.$file; //That is the important path to store your file
$content="This is a dummy text"; //As you read
$draft=fopen($fullpath,"x") or die ('Something went wrong'); //"x" mean only for write if file already exists return error use any flag you need you can use any var name for "$draft" can use for example "$handle" instead
fwrite($draft, $content); //Writes content inside file
fclose($draft); //Close the dummy test file
if (file_exists($fullpath)) { //Verify is file has been created
echo 'File created '."Ok!".'<br>';
} else {
echo 'File do not created function is '."scrubbed!".'<br>';
};
$data=file_get_contents($fullpath); //Verifies if content writed is ok
if ($data==$content) {
echo 'All content is '."Ok!".'<br>';
} else {
echo 'All content is '."scrubbed!".'<br>';
};
来源:https://stackoverflow.com/questions/1303199/php-fopen-relative-path-broken-mystery