Recent Topics

1 Feb 02, 2009 01:16    

My b2evolution Version: 1.10.2

Well, I had a slight problem but then found a way to fix it. I guess I'm putting this here just in case someone finds it useful.

While testing the 3.0 alpha, the thumbnail resizing thingy created a bunch of files owned by `nobody:nobody`. When I went to clean up (remove) my test install, I couldn't remove these files.

I searched the interwebs and found the solution (besides the oft-mentioned suphp advice): use php itself to do things with those files since it will be running as `nobody:nobody`.

For example, the following code will recursively traverse a directory tree and attempt to delete everything. It will not be able to delete anything that it doesn't own... but still, USE THIS CAREFULLY!!!


<?PHP

// Recursively deletes an entire directory tree created by a PHP
// script under the nobody user (you should try an `rm -rf directory`
// before running this to make sure all files to which you do have
// appropriate permissions are deleted).  TO USE: Rename this file
// delete.php, change the directory below (the $todeldir variable) to
// the name of the directory to be removed, save it in the same
// directory as the directory in question and then run it via your web
// browser.

$todeldir = "test";  //directory to delete

//uncomment the block below to give it something to test. Note: the
//directory this is run in must be writeable by the webserver.

/*
$todeldir = "testdir";                          //override above
echo "Creating testdir ... ";
mkdir('testdir', 0777);				//create directory
echo "Done<br/>";
echo "Creating testdir/testdir ... ";
mkdir('testdir/testdir', 0777);			//create another
echo "Done<br/>";
echo "Creating testdir/test.html ... ";
$fh = fopen('testdir/test.html', 'a');		//open file in 1st dir
fwrite($fh, '<h1>Hello world!</h1>');		//write to it
fclose($fh);					//close it
echo "Done<br/>";
echo "Creating testdir/testdir/test.html ... ";
$fh = fopen('testdir/testdir/test.html', 'a');	//open file in 2nd dir
fwrite($fh, '<h1>Hello world!</h1>');		//write to it
fclose($fh);					//close it
echo "Done<br/>";
*/

// unlink('error_log');

recursive_remove_directory($todeldir);
   
// ------------ lixlpixel recursive PHP functions -------------
// recursive_remove_directory( directory to delete, empty )
// expects path to directory and optional TRUE / FALSE to empty
// of course PHP has to have the rights to delete the directory
// you specify and all files and folders inside the directory
// ------------------------------------------------------------

// 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
		    echo "Removing file: $path.<br>";
		    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;
      }
}
// ------------------------------------------------------------

?>


Form is loading...