I once used this wrong PHP script which created a 340 mode directories:
<?php
$uname = "secret";
mkdir("/home/u251526215/public_html/user/profile/".$uname."", 755);
?>
The script above creates a 340 CHMOD directories. I've repaired the "755" to "0755" and it is now working perfectly. But for now, how can I delete the 340 directories which were already created? I have tried to delete them using FTP manager, but it kept saying error. I've tried to use rmdir() but it says directory not empty but it is completely empty!
Updated: All action to the directory; rename, move, copy, change permission and open are error returned
Maybe there is a hidden file. I found this function to delete a directory with all contents:
function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($dirname.'/'.$file);
}
}
closedir($dir_handle);
rmdir($dirname);
return true;
}
Source http://www.ozzu.com/programming-forum/php-delete-directory-folder-t47492.html
Related
I am calling my php function from some other website which delete a folder on my server in background.
This is a function which I am using to delete a folder.
public static function remove($dir)
{
if (is_dir($dir)) {
$diropen = opendir($dir);
while($d = readdir($diropen)) {
if ($d!= '.' && $d != '..') {
self::remove($dir . "/$d");
}
}
#rmdir($dir);
} elseif (is_file($dir)) {
#unlink($dir);
}
}
If I am having three files in folder then it deletes only two and unable to delete last file or unlink fails on last file.
If I am having two files then it deletes only one file.
I have checked writable permission using is_writable it is true for all files.
Can somebody please help me out. or how to debug this behavior as this function is getting called in background.
my directory was open in some other function , so I closedir my folder and then above function working fine.
<?php
function delete_directory($target) {
if (is_dir($target))
$dir_handle = opendir($target);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != "." && $file != "..") {
if (!is_dir($dirname."/".$file))
unlink($dirname."/".$file);
else
delete_directory($target.'/'.$file);
}
}
closedir($dir_handle);
rmdir($target);
return true;
}
?>
use closedir and you'll be fine.
I get this error message when I try to delete a directory:
"Unable to delete the file" using PHP Codeigniter's delete_dir('/path/to/my/dir/'). No additional info provided.
There is no problem with the ftp conn (works with everything else), no problem with the file permission (delete_file() works jsut fine)..no problem with the code, the path is also valid. I'm all out of ideas and open for suggestions.
You can use unlink() to remove php directory.
function removedir($dir1) {
if (is_dir($dir1)) {
//scan
$files = scandir($dir1);
//loop through all the files
foreach ($files as $file) {
if ($file != "." && $file != "..") {
if (filetype($dir1."/".$file) == "dir1")
rrmdir($dir1."/".$file);
else
unlink($dir1."/".$file);
}
}
reset($files);
rmdir($dir);
}
}
I try to delete everything in folder (including subfolders) but:
Warning: unlink(./../kaj-content/theme/one-four): Permission denied in
C:\wamp\www\kaj\kaj-admin\includes\incAppearance.php on line 36
this is my code:
$themeDirectory = './../kaj-content/theme';
$dir = $themeDirectory . '/' . $themeName;
array_map('unlink', glob($dir));
how can I change my code ?
Other code didn't work, like:
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..")
rrmdir("$dir/$file");
rmdir($dir);
} else if (file_exists($dir))
unlink($dir);
}
Your php code is running as a particular user, maybe apache.
Your error means that php does not have the correct permissions for directory one-four.
Check the permissions on that directory. Grant write permission for that directory to the user that php is using. Then, your code will be able to delete the file.
I have main folder named "gallery". In this folder there are some sub folders like "animal", "tree", etc. folder "animal" contains some pictures and so on. suppose i want to delete "animal" folder with all pictures in it, how can do this?
i tried-
rmdirr($_SERVER['DOCUMENT_ROOT']."admin/gallery/animal");
but in server, it deleted the whole "gallery" folder with all in it. please tell me what i am doing wrong and also give me a solution. thanks in advance.
If there are no subfolders you can use glob() to empty the directory before using rmdir():
foreach( glob( "/path/to/dir/*.*" ) as $filename ) {
unlink( $filename );
}
rmdir( "/path/to/dir" );
By the way, rmdir() shouldn't delete any files. It just fails if the directory isn't empty. You might want to make sure there's nothing else in your code that causes the parent directory to be wiped out.
Just delete the directory recursively with a helper function:
rrmdir($_SERVER['DOCUMENT_ROOT']."admin/gallery/animal");
function rrmdir($path)
{
return is_file($path)
? #unlink($path)
: array_map('rrmdir', glob($path.'/*')) == #rmdir($path)
;
}
When removing a desired directory, you must first remove the files within that directory. Once that is complete, then you may remove the directory -- assuming you have the proper permissions.
Included below is code that removes the files in the directory and will then remove the specified directory itself:
<?php
$dirname = "animal";
function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
chmod($dir.$file, 0777);
if(is_dir($dir.$file)) {
chdir('.');
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("Unable to delete $dir$file");
}else{
unlink($dir.$file) or DIE("Unable to delete $dir$file");
}
}
}
closedir($mydir);
}
destroyDir($dirname);
?>
<?php
function rmdirr($dirname){
// Sanity check
$dirname = "TEST/";
if (!file_exists($dirname)) {
return false;
}
// Simple delete for a file
if (is_file($dirname)) {
return unlink($dirname);
}
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") {
continue;
}
// Recurse
unlink("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}
if (rmdirr($_GET['map'])){
echo "TEST FERo";
}
else{
echo "something went wrong.";
}
?>
Its working well and good. But if i need to delete the folder which contains some files and empty folder. In this case it delete all the files but not the empty folder. It throws an exception likeā¦
Warning: unlink(TEST//New Folder) [function.unlink]: Permission denied
in E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 23
Warning: rmdir(TEST/) [function.rmdir]: Directory not empty in
E:\Xampp\xampp\htdocs\delete_FILE\delete_FILE.php on line 28
What is the possible way to delete even the folder is empty.
Just use your rmdirr function recursively in the while loop instead of unlink;
function rmdirr($dirname){
// Sanity check
if (!file_exists($dirname)) { return false; }
// Simple delete for a file
if (is_file($dirname)) { return unlink($dirname); }
// Loop through the folder
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == "." || $entry == "..") { continue; }
// Recurse
rmdirr("$dirname/$entry");
}
// Clean up
$dir->close();
return rmdir($dirname);
}
This way it'll also take care of non-empty subfolders...
you have to make sure your webserver is able to delete those files. check the permissions.
You are deleting the files in 1 level only. Your code tries to delete the folder TEST//New Folder using unlink instead of rmdir. You have to check if it's a folder or not, then rmdir or unlink it.