View Full Version : deleting files and directories ..
JaeSun
05-04-2005, 10:51 PM
ok, couple questions ...
if im trying to delete a file that doesnt exist, then the unlink() returns false ...
but at the same time, it spits out an error message:
Warning: unlink(/directory/to/file): No such file or directory in /directory/to/script.php on line 57
Warning: unlink(/directory/to/file): No such file or directory in /directory/to/script.php on line 57
how do i get rid of that?
also.... i would like to delete a directory if its empty...how do i check to see if a directory is empty? looking at the manual, dont see how they check (it just seems to check if the target is a file or directory?) .....
i could jsut try and delete the folder and see if i get a false return? but then it might spit out an error like that above??
any help would be appreciated
walterquez
05-04-2005, 11:03 PM
Will adding a "@" to a command suppress the error message? Instead of unlink(), you can use @unlink()?
I could be wrong.
JaeSun
05-05-2005, 12:03 AM
ya, i just found that out ...
now, how to check if a directory is empty ...
walterquez
05-05-2005, 12:19 AM
Well, if you're using UNIX it will not let you delete a directory if it contains files. Otherwise you can use$dirname = "mydirectory";
if (file_exists($dirname))
{
rmdir($dirname);
}
JaeSun
05-05-2005, 12:24 AM
thus, checking for whether its empty or not ....
here is my script .. (pseudocode):
1. user selects files he wants to delete, (files stored in folders based on date)
2. script goes through and deletes each file
3. before finishing, to not have tons of empty directories after awhile, checks if the directory that the files was just deleted from is empty. if its empty, delete the folder also.
i could just leave it out and make a seperate script for the user to do every once in awhile as part of maintenance, but, then id have to create a recursive script....
i think i just found out how though...glob
JaeSun
05-05-2005, 12:26 AM
Well, if you're using UNIX it will not let you delete a directory if it contains files. Otherwise you can use$dirname = "mydirectory";
if (file_exists($dirname))
{
rmdir($dirname);
}
uhhh, thats not checking if the directory is empty ... i can do that and have a directory full of files .. but it wont check that..instead, itll check if the directory itself exists (which will always return true in that case) ....
http://us2.php.net/file_exists
walterquez
05-05-2005, 12:32 AM
Sorry, you can check out this link
http://us2.php.net/rmdir
walterquez
05-05-2005, 12:35 AM
But again, the directory must be empty in order to delete it in unix, otherwise it won't.
If the directory is empty, it will get deleted just as you requested. But if it is not empty, it will not.
JaeSun
05-05-2005, 12:46 AM
please read the file_exists manual ...
http://www.whiteazn.com/fbc/test/directory_delete.php
tries to delete
http://www.whiteazn.com/fbc/test/directory/
the code of directory.php
<?php
$dirname = "/home/whiteazn/public_html/fbc/test/directory/";
if (file_exists($dirname))
{
rmdir($dirname);
}
?>
your code as you printed.... wont delete cuz its not empty!!
file_exists is checking to see if the directory or file exists (in this case, and the case you present, the DIRECTORY /home/whiteazn/public_html/fbc/test/directory/ ) ...
does the directory exist?
yes ..
tries to rmdir ...
cant, because a file exists in the directory....
JaeSun
05-05-2005, 12:48 AM
to see if a directory is empty, i just made this function:
function emptyDir( $dir, $files )
{
return glob($dir . $files);
}
$check_dir = emptyDir($dir, "*.*");
if( empty($check_dir) )
{
if( !(rmdir($dir)) )
{
echo "directory couldnt be deleted for other reasons such as permission problems";
}
}
that will do it...
your code will not
obviously, the naming of the functions is a little misleading, but that can easily be changed...
scoutt
05-05-2005, 03:31 PM
why can't you do this?
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
if it is false then it will be empty. you want to readdir not file_exist.
Horus_Kol
05-06-2005, 02:35 AM
using the @ to suppress messages is not always desirable - why don't you configure the php.ini settings so that you don't send error messages to the browser (except your own custom ones, of course) but they are still written to the error log file?
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.