简体   繁体   中英

File Directory and delete file php?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM