Jump to content

.DS_STORE and .ftpqutoa not unlinking?


Go to solution Solved by requinix,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/279406-ds_store-and-ftpqutoa-not-unlinking/
Share on other sites

  • Solution

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).

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.