Senior47 Posted March 20, 2008 Share Posted March 20, 2008 This code loops through a folder, looking for the last modified text file. It also hits folders and .. which can't be split into $name and $ext... and that produce this error. ( well, I think that's the reason - but could be wrong! ) I can't find any way to fix it, so I'm looking for some help/suggestions here;-) The code: function mostRecentModifiedFileTime($dirName,$doRecursive) { global $newest, $newest_artist; $d = dir($dirName); $lastModified = 0; while($entry = $d->read()) { list($name,$ext) = split("\.", $entry,2); if ($entry != "." && $entry != ".." && $ext != "m3u" && $ext != "jpg" && $ext != "txt" && $ext != "php" && $ext = "mp3") { if (!is_dir($dirName."/".$entry)) { clearstatcache('fileatime'); $currentModified = filemtime($dirName."/".$entry); } else if ($doRecursive && is_dir($dirName."/".$entry)) { $currentModified = mostRecentModifiedFileTime($dirName."/".$entry,true); } if ($currentModified > $lastModified){ $lastModified = $currentModified; } } if ($lastModified > $newest ){ $newest = $lastModified; $newest_artist = $dirName."/".$entry; } } $d->close(); return $lastModified; } any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/ Share on other sites More sharing options...
Darklink Posted March 20, 2008 Share Posted March 20, 2008 You might want to alter your error_reporting values, or if you want to sort it out via your code there, you need to define each array key before-hand. It's really annoying sometimes, but it's all down to security. Edit: so before $name and $ext is made do: $name = ''; $ext = ''; Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496831 Share on other sites More sharing options...
Senior47 Posted March 20, 2008 Author Share Posted March 20, 2008 You might want to alter your error_reporting values, or if you want to sort it out via your code there, you need to define each array key before-hand. I'm just trying to sort out errors in the code, and this is... well, annoying!-( $name = ''; $ext = ''; I've tested it, but the errors are the same! Thanks fore your answer anyway!-) Another solution? Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496869 Share on other sites More sharing options...
BlueSkyIS Posted March 20, 2008 Share Posted March 20, 2008 "You might want to alter your error_reporting values" before any other code, put this: error_reporting(E_ERROR | E_WARNING | E_PARSE); Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496901 Share on other sites More sharing options...
Senior47 Posted March 20, 2008 Author Share Posted March 20, 2008 Taking away error messages doesn't remove the errors, just hiding them. I trying to get rid of the errors... Thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496910 Share on other sites More sharing options...
BlueSkyIS Posted March 20, 2008 Share Posted March 20, 2008 that's not an error, it's a notice. you'll see a boat-load more of them if you don't turn them off. Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496932 Share on other sites More sharing options...
wildteen88 Posted March 20, 2008 Share Posted March 20, 2008 function mostRecentModifiedFileTime($dirName, $doRecursive) { global $newest, $newest_artist; $d = dir($dirName); $lastModified = 0; while($entry = $d->read()) { if ($entry != "." && $entry != "..") { list($name,$ext) = split("\.", $entry,2); if($ext != "m3u" && $ext != "jpg" && $ext != "txt" && $ext != "php" && $ext = "mp3") { if (!is_dir($dirName."/".$entry)) { clearstatcache('fileatime'); $currentModified = filemtime($dirName."/".$entry); } elseif ($doRecursive && is_dir($dirName."/".$entry)) { $currentModified = mostRecentModifiedFileTime($dirName."/".$entry, true); } if ($currentModified > $lastModified) { $lastModified = $currentModified; } } } if ($lastModified > $newest ) { $newest = $lastModified; $newest_artist = $dirName."/".$entry; } } $d->close(); return $lastModified; } that's not an error, it's a notice. you'll see a boat-load more of them if you don't turn them off. Even though they are not errors you should still try to correct them. Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496943 Share on other sites More sharing options...
BlueSkyIS Posted March 20, 2008 Share Posted March 20, 2008 i don't think so. there's no way i'm going to spend all day tracking down undefined indexes. for what? Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496954 Share on other sites More sharing options...
wildteen88 Posted March 20, 2008 Share Posted March 20, 2008 i don't think so. there's no way i'm going to spend all day tracking down undefined indexes. for what? If you are getting underfined indexes then your style of programming is floored. You should treat indexes the same as variables. You wont use a variable before you use it. Sorting out notices is usually quite easy. However just ignoring them is not an option that should the last resort. Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-496981 Share on other sites More sharing options...
Senior47 Posted March 20, 2008 Author Share Posted March 20, 2008 If you are getting undefined indexes then your style of programming is floored. You should treat indexes the same as variables. You wont use a variable before you use it. Sorting out notices is usually quite easy. However just ignoring them is not an option that should the last resort. Thank you for your help, nearly missed your code!-/ Sadly it didn't bring me any further though! Maybe the code is "bad" from the beginning? The code goes through a folder "A", find a lot of subfolders "B" which contains a lot of new folders "C". In the "C" folders it should find the last modified file (in this example, mp3's - not txt as I wroth in the start ) I think the folders "A" and "B" is the reason for the error, but if i "jump over" them, I don't get the path to the file, just the file. ( I get an notice in another part of the code - which use the result from the above code. The reason I only get 2 errors here is because this is on a testserver with just one folder to search through!-/ Checking it out "live" (enabling error logging) filled my error log immediately !-/ Possibly I should start all over, try to find some other way to achieve the same result! Any ideas are welcome, thanks anyway!-) Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-497137 Share on other sites More sharing options...
Senior47 Posted March 21, 2008 Author Share Posted March 21, 2008 Problem solved! A little (!!!) rewrite of the code and no notice! The code follows - if anyone is interested!-) function mostRecentModifiedFileTime($dirName, $doRecursive = true) { global $newest, $newest_artist; $d = dir($dirName); $lastModified = 0; $currentModified = 0; while($entry = $d->read()) { if ($entry == '.' || $entry == '..') { continue; } if (!is_dir($dirName . '/' . $entry)) { list($name, $ext) = split('\.', $entry, 2); if ($ext == 'mp3') { clearstatcache(); $currentModified = filemtime($dirName . '/' . $entry); } } else { $currentModified = mostRecentModifiedFileTime($dirName . '/' . $entry); } $lastModified = max($lastModified, $currentModified); if ($lastModified > $newest ){ $newest = $lastModified; $newest_artist = $dirName."/".$entry; } } $d->close(); return $lastModified; So easy to suppress those PHP notice, but I feel more happy when they are all gone!-) Now, lets see... is there any more Big thanks to "wildteen88" code, it pushed me in the right direction! Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-497882 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 i don't think so. there's no way i'm going to spend all day tracking down undefined indexes. for what? If you are getting underfined indexes then your style of programming is floored. You should treat indexes the same as variables. You wont use a variable before you use it. Sorting out notices is usually quite easy. However just ignoring them is not an option that should the last resort. i'm sure you'll find that the vast majority of PHP developers believe otherwise. but to each his own, of course. but then again, your statements make sense, and now I must figure out what to do to prevent undefined indexes. Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-497883 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 here is a notorious notice: Notice: Undefined index: item in /Users/lesbrown/Sites/site1/test.php on line 2 from this: <? if ($_POST['item'] == "test") { echo "item is equal to test!"; } ?> i always check $_POST vars this way. how do you prevent the Undefined index notice in this situation? answered my own question, i think. i used isset($_POST['item']) to ensure the index is defined before running a comparison operator on it. THIS is going to take some getting use to. Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-497887 Share on other sites More sharing options...
wildteen88 Posted March 21, 2008 Share Posted March 21, 2008 Yes always use isset when accessing $_POST vars, eg: if (isset($_POST['item']) && $_POST['item'] == "test") { echo "item is equal to test!"; } Quote Link to comment https://forums.phpfreaks.com/topic/97100-php-notice-undefined-offset/#findComment-497915 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.