0

I'm trying to create php script to scan directory and delete file in this directory. I have problem with my scanning file extension is not working right

<?php 
 if ($handle = opendir(''))
 {
    echo " Directory handle: $handle \n";
    echo "Entries: \n";


    while (false !== ($entry = readdir($handle))) 
    {
        echo" $entry :\n";
        $file_parts = pathinfo($entry);

        switch ($file_parts['extension']) 
        {
            case 'dmg':
                echo "dmg";
                break;

            default:
                echo "no file";
                break;

        }


    }

    closedir($handle);

}


?>

Notice: Undefined index: extension in /Applications/MAMP/htdocs/dir.php on line 13

1
  • I update error Notice: Undefined index: extension in /Applications/MAMP/htdocs/dir.php on line 13 Commented Mar 24, 2013 at 2:04

2 Answers 2

1

This error would be caused if for example the path doesn't have an extension or some error occured.

If you want to retreive the filetype of the files it seems like a roundabout way of doing so when you can just do

switch(filetype($dir.$file)){
      case 'dmg' ://And so on
}

Here $dir would be /Users/username/Downloads

Sign up to request clarification or add additional context in comments.

1 Comment

unlink($dir.$file) should be what your looking for
1

Undefined Index means it doesn't exists in the array.

From docs:

If the options parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename.

Your files do have extension?

Also, put '.' (dot) in your dir instead of blank space.

You can use var_dump( $file_parts ); to see what it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.