Could not delete file in php using rmdir() - php

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

Related

But it still deletes how to delete a directory using PHP

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

How to delete all files and folder in php with permission?

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.

php scandir() - Invalid argument supplied for foreach() [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);
}
}

File Directory and delete file php?

I'm trying to create php script to scan directory and delete file in this directory. I have problem with my scanning file extension is not working right
<?php
if ($handle = opendir(''))
{
echo " Directory handle: $handle \n";
echo "Entries: \n";
while (false !== ($entry = readdir($handle)))
{
echo" $entry :\n";
$file_parts = pathinfo($entry);
switch ($file_parts['extension'])
{
case 'dmg':
echo "dmg";
break;
default:
echo "no file";
break;
}
}
closedir($handle);
}
?>
Notice: Undefined index: extension in /Applications/MAMP/htdocs/dir.php on line 13
This error would be caused if for example the path doesn't have an extension or some error occured.
If you want to retreive the filetype of the files it seems like a roundabout way of doing so when you can just do
switch(filetype($dir.$file)){
case 'dmg' ://And so on
}
Here $dir would be /Users/username/Downloads
Undefined Index means it doesn't exists in the array.
From docs:
If the options parameter is not passed, an associative array containing the
following elements is returned: dirname, basename, extension (if any), and filename.
Your files do have extension?
Also, put '.' (dot) in your dir instead of blank space.
You can use var_dump( $file_parts ); to see what it is.

Deleting a 340 mode directories

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

Categories