user2101411
user2101411

Reputation: 1202

PHP is not removing files due to directory or files not being found

I'm running into a bit of an issue. I'm trying to remove all files from a directory and then remove the directory itself as rmdir won't delete an non-empty directory. However, when I try, it is saying it cannot find the files specified even though it is returning them with the glob function.

Here is my code:

 $store_files = glob(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name . '/*.{jpg,jpeg,png}', GLOB_BRACE);
 $category_files = glob(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' .  $store_name . '/' . $category . '/items/*.{jpg,jpeg,png}', GLOB_BRACE);



 foreach ($category_files as $cfiles) {
    unlink(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name . '/' . $cfiles);
 }

 foreach ($store_files as $file) {
    unlink(realpath(getcwd()) . '/public/images/uploads/store/' .  $owner_id . '/' . $store_name . '/' . $category . '/items/' . $file);
 }


 rmdir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name);

 return true;

but the error message I am getting in the logs is:

    [Tue Jul 30 13:35:04.611780 2019] [php7:warn] [pid 12952:tid 1944] [client ::1:59713] PHP Warning:  unlink(C:\\xampp\\htdocs/public/images/uploads/store/3/Jimmy's Store/Computers/items/C:\\xampp\\htdocs/public/images/uploads/store/3/Jimmy's Store/Jimmy's_Store_cover.jpg): No such file or directory in C:\\xampp\\htdocs\\module\\Application\\src\\Application\\Classes\\User.php on line 494, referer: http://localhost/user
[Tue Jul 30 13:35:04.611780 2019] [php7:warn] [pid 12952:tid 1944] [client ::1:59713] PHP Warning:  rmdir(C:\\xampp\\htdocs/public/images/uploads/store/3/Jimmy's Store): Directory not empty in C:\\xampp\\htdocs\\module\\Application\\src\\Application\\Classes\\User.php on line 498, referer: http://localhost/user

I'm not sure why it is doing this to be honest.

Any help would be appreciated.

Oh, I did check the paths and that is the path to the directory + files.

Thanks!

Update:

Per the one answer to my question (which did work, as I suspected), it only removes from one directory but I need to remove files from multiple sub directories then remove the root directory. Here is the code I currently have in place:

 // then delete the files inside the category/items directory then remove the store folder
$store_files = glob(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name . '/*');

$category_files = glob(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name . '/' . $category . '/items/*');

foreach ($category_files as $file) {
    if (is_file($file)) {
        unlink($file);
    }
}

foreach ($store_files as $files) {
    if (is_file($files)) {
        unlink($files);
    }
}


rmdir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name);

For some reason, it is not deleting the files located in the $category/items/ directory, thus I am running into the rmdir not empty PHP error. I know that path is correct, which is even more confusing. For some reason, it is returning an empty array with the files inside the category_name/items/ directory even though there is files inside the items directory.

Upvotes: 1

Views: 102

Answers (1)

R10t--
R10t--

Reputation: 828

When you loop through the file paths, you're appending $cpath to the other path. When looping the files returned, $cpath is a full path and doesn't need to be appended to folder locations. Try just doing

foreach ($category_files as $cfiles) {
    unlink($cfiles);
 }

Updated:

It looks like you're trying to remove all files within a directory as well as the root folder. In order to remove the folder, all files within it must be deleted. Based on this article, you can do a recursive loop on all files and directories within a folder to delete everything in a folder. The following code should work to delete everything in the folder and then remove the folder. You shouldn't need two for loops to delete files.

// Get the directory with * selector for all files
$deleteDirectory = glob(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name . '/*');

//Loop through the file list.
foreach($deleteDirectory as $file){
    //Make sure that this is a file and not a directory.
    if(is_file($file)){
        //Use the unlink function to delete the file.
        unlink($file);
    }
}

//Finally, remove the directory
rmdir(realpath(getcwd()) . '/public/images/uploads/store/' . $owner_id . '/' . $store_name)

Upvotes: 2

Related Questions