redge@tvdata.be Posted March 5, 2012 Share Posted March 5, 2012 Hi, this works: if(file_exists("myFile.txt")){etc.} this doesn't: $myFile = "myFile.txt"; if(file_exists($myFile)){etc.} Can anybody explain this to me? I'd really like to pass the filename as a variable... Kind regards, R. Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/ Share on other sites More sharing options...
AyKay47 Posted March 5, 2012 Share Posted March 5, 2012 That will work, something else must be going on if that is not working. Please post the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324126 Share on other sites More sharing options...
redge@tvdata.be Posted March 5, 2012 Author Share Posted March 5, 2012 <?php $mathSportFile = "mathSportFile.xml"; $timeFile = "timeFile.txt"; function compareTimeStamps(){ if (file_exists("timeFile.txt")){ $oldTime = file_get_contents("timeFile.txt"); }else{ $oldTime = date("F d Y H:i:s", filemtime("timeFile.txt")); } $newTime = date("F d Y H:i:s", filemtime("mathSportFile.xml")); if ($oldTime == $newTime){ $echoRes = 'the same'; }else{ $echoRes = 'different'; $fh = fopen("timeFile.txt", 'w') or die("can't open file"); fwrite($fh, $newTime); fclose($fh); } echo $echoRes; } compareTimeStamps(); ?> if you change any of these filenames by the variable (top) it doesn't work... I was convinced it would work as well... Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324129 Share on other sites More sharing options...
redge@tvdata.be Posted March 5, 2012 Author Share Posted March 5, 2012 Does it make a difference if the page is called by an AJAX script with "xmlHttp"? I'm convinced it shouldn't but unexplainable errors make you doubt everything... Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324133 Share on other sites More sharing options...
redge@tvdata.be Posted March 5, 2012 Author Share Posted March 5, 2012 Still trying to find out what's going on... when I put the variables further down in the script e.a. not at the top, it does work. Who can explain this? Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324138 Share on other sites More sharing options...
AyKay47 Posted March 5, 2012 Share Posted March 5, 2012 this is an issue dealing with variable scope You will either need to introduce the variables inside of the functions local scope, or pass them in as function parameters. Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324148 Share on other sites More sharing options...
redge@tvdata.be Posted March 5, 2012 Author Share Posted March 5, 2012 Thanks, works fine when I pass the variables to the function... I was convinced variables declared outside the function would have a "global" scope !?# Apparently not... Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324151 Share on other sites More sharing options...
AyKay47 Posted March 5, 2012 Share Posted March 5, 2012 Thanks, works fine when I pass the variables to the function... I was convinced variables declared outside the function would have a "global" scope !?# Apparently not... No they do not, and do not get into the habit of using the global keyword. Using it should always be avoided. Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324153 Share on other sites More sharing options...
redge@tvdata.be Posted March 5, 2012 Author Share Posted March 5, 2012 I will obey!! Thanks a million, Regards, Redge Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324155 Share on other sites More sharing options...
Adam Posted March 5, 2012 Share Posted March 5, 2012 Thanks, works fine when I pass the variables to the function... I was convinced variables declared outside the function would have a "global" scope !?# Apparently not... JavaScript behaves that way. Could be what's throwing you off? Quote Link to comment https://forums.phpfreaks.com/topic/258316-pass-filename-as-variable-error/#findComment-1324198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.