eldan88 Posted June 21, 2013 Share Posted June 21, 2013 Hey, I have wrote some code to unlink all the files in my directory and then delete the directory. All my files on my remote server gets unlinked besides the following 2 following files . DS_STORE and .ftpqutoa. Below is the code that I have written to unlink all files and delete all the code. Any ideas on how to solve this? All help is highly appreciated. Thanks! ?php if(isset($_POST['submit'])) { $store = $_POST['id']; // assign the store ID to the $store variable foreach ($store as $id) { // Run a foreach loop to get the store id $query = "SELECT * FROM stores WHERE id = {$id} "; // Run the query for the specific store id $result_query = mysql_query($query,$connection); // Execute the query $result = mysql_fetch_array($result_query,$connection); // Run a mysql fetch array on the result query $store_name = $result['store_name']; // get the store name $menus_dir_path = "stores/".$store_name;// Path to the menus assets $array_map_menus= array_map("unlink", glob($menus_dir_path."/*.*")); //rmdir($menus_dir_path); }// end of if(isset($_POST['submit'])) { on line 13 }// end of foreach ($store as $store_id) { on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/ Share on other sites More sharing options...
requinix Posted June 21, 2013 Share Posted June 21, 2013 (edited) Dot/hidden files do not match against the *.* pattern. Try just * Edited June 21, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437167 Share on other sites More sharing options...
eldan88 Posted June 21, 2013 Author Share Posted June 21, 2013 Dot/hidden files do not match against the *.* pattern. Try just * Okay got it. Will do. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437278 Share on other sites More sharing options...
requinix Posted June 21, 2013 Share Posted June 21, 2013 (edited) Forget what I said. Since you want all files in the directory use scandir instead of glob(). It will also include "." and ".." so you'll need to filter those out. Edited June 21, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437295 Share on other sites More sharing options...
eldan88 Posted June 22, 2013 Author Share Posted June 22, 2013 Forget what I said. Since you want all files in the directory use scandir instead of glob(). It will also include "." and ".." so you'll need to filter those out. Thanks for the response...How can I filter them out?? Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437333 Share on other sites More sharing options...
Solution requinix Posted June 22, 2013 Solution Share Posted June 22, 2013 You can't delete directories with unlink() anyways so is_file() should work for it. Turns out glob() is still easier to use after all - mostly because you're looking at files in a different directory. Since I've now changed my mind a second time I'll present to you a bunch of options: If you can use GLOB_BRACE you can $menus_dir_path = "stores/".$store_name;// Path to the menus assets array_map("unlink", array_filter(glob($menus_dir_path . "/{*.*,.*}", GLOB_BRACE), "is_file"));If not you can array_merge(). $menus_dir_path = "stores/".$store_name;// Path to the menus assets array_map("unlink", array_filter(array_merge(glob($menus_dir_path . "/*.*"), glob($menus_dir_path . "/.*")), "is_file"));Or you can loop over a FilesystemIterator. foreach (new FilesystemIterator($menus_dir_path) as $file) { if ($file->isFile()) { unlink($file->getPathname()); } }Or something similar with scandir() foreach (scandir($menus_dir_path) as $file) { if (is_file($menus_dir_path . "/" . $file)) { unlink($menus_dir_path . "/" . $file); } }which you can convert to an anonymous function and pass to array_map() (something you couldn't do with the FilesystemIterator). Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437334 Share on other sites More sharing options...
eldan88 Posted June 23, 2013 Author Share Posted June 23, 2013 You can't delete directories with unlink() anyways so is_file() should work for it. Turns out glob() is still easier to use after all - mostly because you're looking at files in a different directory. Since I've now changed my mind a second time I'll present to you a bunch of options: If you can use GLOB_BRACE you can $menus_dir_path = "stores/".$store_name;// Path to the menus assets array_map("unlink", array_filter(glob($menus_dir_path . "/{*.*,.*}", GLOB_BRACE), "is_file"));If not you can array_merge(). $menus_dir_path = "stores/".$store_name;// Path to the menus assets array_map("unlink", array_filter(array_merge(glob($menus_dir_path . "/*.*"), glob($menus_dir_path . "/.*")), "is_file"));Or you can loop over a FilesystemIterator. foreach (new FilesystemIterator($menus_dir_path) as $file) { if ($file->isFile()) { unlink($file->getPathname()); } }Or something similar with scandir() foreach (scandir($menus_dir_path) as $file) { if (is_file($menus_dir_path . "/" . $file)) { unlink($menus_dir_path . "/" . $file); } }which you can convert to an anonymous function and pass to array_map() (something you couldn't do with the FilesystemIterator). The first code (GLOB_BRACE) did the job! Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/#findComment-1437456 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.