Jump to content

Archived

This topic is now archived and is closed to further replies.

bizerk

Trying to Recursively Delete files from Directory, but function undefined??

Recommended Posts

Okay well i finally got my hands on a working recursive delete directory function, but now when i try to use it it says its UNDEFINED?

here is what i have

[size=9pt]// to use this function to totally remove a directory, write:
// recursive_remove_directory('path/to/directory/to/delete');

// to use this function to empty a directory, write:
// recursive_remove_directory('path/to/full_directory',TRUE);


function recursive_remove_directory($directory, $empty=FALSE)
{
// if the path has a slash at the end we remove it here
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}

// if the path is not valid or is not a directory ...
if(!file_exists($directory) || !is_dir($directory))
{
// ... we return false and exit the function
return FALSE;

// ... if the path is not readable
}elseif(!is_readable($directory))
{
// ... we return false and exit the function
return FALSE;

// ... else if the path is readable
}else{

// we open the directory
$handle = opendir($directory);

// and scan through the items inside
while (FALSE !== ($item = readdir($handle)))
{
// if the filepointer is not the current directory
// or the parent directory
if($item != '.' && $item != '..')
{
// we build the new path to delete
$path = $directory.'/'.$item;

// if the new path is a directory
if(is_dir($path))
{
// we call this function with the new path
recursive_remove_directory($path);

// if the new path is a file
}else{
// we remove the file
unlink($path);
}
}
}
// close the directory
closedir($handle);

// if the option to empty is not set to true
if($empty == FALSE)
{
// try to delete the now empty directory
if(!rmdir($directory))
{
// return false if not possible
return FALSE;
}
}
// return success
return TRUE;
}
}[/size]

Alright so it says the way to delet all files from a directory is to call the function and than put the path, TRUE

so here is what i did:

[size=9pt]function delete($user_dat, $globalvars)
{
recursive_remove_directory('users' . "/" . $user_dat['name'], TRUE);
}[/size]

so when i call the fucntion delete from a button or whatever it hsould REcursively remove directories RIGHT!? well wrong it says

[size=9pt]Fatal error: Call to undefined function recursive_remove_directory() in /home/mysite:]lol/www/upload.php on line 1057[/size]

Line 1057 is the Function Delete line btw. I have no idea what i am doing wrong. Please someone explain to me why the function isnt being defined...

Share this post


Link to post
Share on other sites
Don't use recursion, stay in the function and exit it only when your done. Recursion is never the best way in PHP, because of how PHP uses the memory stack to do recursion.

A simple example!

[code=php:0]
<?

function remove_all ( $base )
{
$base = array ( $base );

// delete directory container

$maps = array ();

while ( ! empty ( $base ) )
{
$next = array_pop ( $base );

foreach ( glob ( $next . '*' ) AS $item )
{
if ( ! is_dir ( $item ) )
{
/* remove each file */

unlink ( $item );
}
else
{
/* add the directory so we look in it */

$base[] = $item . '/';

/* keep directories, working backwards */

array_unshift ( $maps, $item . '/' );
}
}
}

/* we cleared the directories, now remove the directories */

array_map ( 'rmdir', $maps );
}

// usage

/* remove all files and directories inside this directory */

// must end with trailing -> /

$path = './images/';

remove_all ( $path );

?>
[/code]


me!

Share this post


Link to post
Share on other sites
but why would recursive not work? It's worked with other things i have done, but for some reason it won't work now... Oh and i tried your script method and it didn't work. It said expecting T_FUNCTION on the line : $path = './images/';

But i would rather stick to recursion. CAn anyone help me :]!

Share this post


Link to post
Share on other sites

×

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.