I have a PHP script for deleting a directory. This deletes the target directory but shows an error message if there are more than one file but it still deletes the directory..weird..;Shown error given below:-
Warning: rmdir(uploads/dd4a96d6907035a1d011b9394d779d3c) [function.rmdir]: Directory not empty in /home/.../public_html/deletepost.php on line 21
here's php
<?php
$dir = $row['album_path'];
17 foreach(scandir($dir) as $file) {
18 if ('.' === $file || '..' === $file) continue;
19 if (is_dir("$dir/$file")) rmdir_recursive("$dir/$file");
20 else unlink("$dir/$file");
21 rmdir($dir);
22 }
?>
Am I doing anything wrong with the code?
Here is an example to delete folder without warning in PHP
http://en.kioskea.net/faq/793-php-warning-rmdir-directory-not-empty
Related
When I try to delete empty directory I got the following warnings for on line 93 and on line 99:
Warning: unlink(/data/sites/web/xxx/www/uploads/file/2021-04-02/.nfs0000000059f70c14000017a9): Device or resource busy in /data/sites/web/xxx/www/dreq/req.php on line 93
Warning: rmdir(/data/sites/web/xxx/www/uploads/file/2021-04-02): Directory not empty in /data/sites/web/xxx/www/dreq/req.php on line 99
here is the delete function :
function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full); // On line 93
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src); // On line 99
}
What is the issue here?
Issue 1. Check which process hold that file with lsof -f | grep filename to check which process hold that resource.
Issue 2. Check the manual rmdir, the directory must be empty.
Attempts to remove the directory named by dirname. The directory must
be empty, and the relevant permissions must permit this. A E_WARNING
level error will be generated on failure.
Refer to this to has a view on where the hidden nfs file comes from
I have these errors when running my script.
failed to open dir: No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: scandir(): (errno 2): No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: array_diff(): Argument #1 is not an array in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 7
Well this is my code below, I don't understand why it failed to open my dir when it's being declared below? Can someone help me with this.
Code of my word2.php
<?php
$numargs = count($argv);
if ($numargs > 1) {
$folder = $argv[1];
echo "Folder is: " . $folder . "\n";
$files = array_diff(scandir($folder), array('.', '..')); //line 6
foreach ($files as $file) { //line 7
$filename = str_replace("í»", "", $filename);
}
} else {
echo "You need to pass the folder absolute path";
exit();
}
Code for running my script using this command ./run.bat this is the filename with a code below.
php word2.php "/Applications/MAMP/htdocs/sites-store/word/images"
PAUSE
Try changing your .bat file to
php word2.php -- "/Applications/MAMP/htdocs/sites-store/word/images"
PAUSE
and you should also do var_dump($argv), to see how your script gets its parameter.
failed to open dir: No such file or directory in
/Applications/MAMP/htdocs/sites-store/word/word2.php on line 6
Make sure that your file in that directory.
Check the filename again means is there any spelling mistakes or duplicated.
Make sure if it's read that directory or not
OK I'm confused. This code is showing me an error but at same time is doing what I actually needed.
$dir = new DirectoryIterator('./');
foreach ($dir as $filename) {
if (mimes_content_type($filename) == 'txt') {
rename($filename, './txt/'.$filename);
}
}
Warning: rename(txt,./txt/txt.txt): Invalid argument in ./www/session/test.php on line 78
Some advices what i did wrong ?
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 a server running PHP Version 5.4.16 and am trying to use scandir to list files within a directory. I am having a hard time figuring out what the issue is. I've tried both ../store/dir_to_scan and /root/store/dir_to_scan. I've also tried using both glob and scandir as you can see below both to no avail. If I remove the dir_to_scan directory it will list the directories inside of /root/store which is what I find most puzzling of all. I've also chmod'd the folders and files to 777 just to make sure it wasn't a permissions issue. I receive an error of "Array ( [type] => 2 [message] => Invalid argument supplied for foreach() [file] => /root/html/test.php [line] => 5 )" also upon running with correct directory setup.
Thanks for any help.
Directory Setup
/root/html //where php script is run
/root/store/dir_to_scan //files I need to list
PHP Script
<?
#$files = glob('../store/dir_to_scan/*', GLOB_BRACE);
$files = scandir("/root/store/dir_to_scan/");
foreach($files as $file) {
//do work here
if ($file === '.' || $file === '..') {
continue;
}
echo $file . "<br>";
}
print_r(error_get_last());
?>
this might be silly but try supplying a trailing slash at the end of:
/root/store/dir_to_scan ->
$files = scandir("/root/store/dir_to_scan/");
this should to solve your problem
$carpeta= "/root/store/dir_to_scan";
if(is_dir($carpeta)){
if($dir = opendir($carpeta)){
while(($file = readdir($dir)) !== false){
if($file != '.' && $file != '..' && $file != '.htaccess'){
echo $file; //or save file name in an array..
// $array_file[] = $file;
}
}
closedir($dir);
}
}