Search code examples
phpfilefile-permissionsunlink

PHP - How to unlink a file with no file extention


This work great to delete php, txt and images but I recently starting using an image resize that inserts temp images in to my trash folder.

* example name 2616cf442b6cd3e1313161551fad6078 *

File type: text/x-generic

Permission: 0644

Tried renaming to .txt with no luck

$cleantrash =(DOCROOT."/woobe/myfiles/trash/"); 
$cleantrash = opendir($cleantrash);

while (($filex_clean = readdir($cleantrash)) !== false) {
if($filex_clean != "." && $filex_clean != ".." && $filex_clean != "index.php") {


        echo "<b>Cleaing Trash...</b> $filex_clean<br>";
        unlink($filex_clean);
        }
}
closedir($cleantrash);

*EDIT *

$newname = basename($filename, ".bmp").".jpg";
rename($filename, $newname);

and the files are gone lol. No unlink useds? so where did they go?


Solution

  • The readdir() function returns file names without their paths (relative to the resource directory parameter you pass it).

    The unlink() function expects the full file path. My guess would be that you need to save the $cleantrash path instead of overwriting it with the resource from opendir(), and then do something like:

    unlink($cleantrash . $filex_clean);