Jump to content

PHP Notice: Undefined offset


Senior47

Recommended Posts

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?

Link to comment
Share on other sites

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    = '';

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!-)

 

 

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.  :D

Link to comment
Share on other sites

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.  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.