PHP: rmdir not working

Associate
Joined
27 Jun 2008
Posts
1,538
This is driving me nuts. I have some functions to make and delete directories which I've used for ages and had no problems with. The rmdir function I have is a little different as it has to first delete all the files inside. However in this one case it just won't work and I'm getting warnings every time. It keeps saying the folder isn't empty but it always is when I check it with an FTP client.

Here are the functions I use.
PHP:
function vmd_mkdir($dir)
{
	mkdir($dir, 0777, true);
	if(!is_dir($dir))
	{
		die('Unable to create directory \''.$dir.'\'');
	}
}
function vmd_rmdir($dir)
{
	if($handle = opendir($dir))
	{
		while(false !== ($obj = readdir($handle)))
		{
			if($obj != '.' && $obj != '..')
			{
				if(filetype($dir.'/'.$obj) == 'dir') vmd_rmdir($dir.'/'.$obj);
				else unlink($dir.'/'.$obj);
			}
		}
		closedir($handle);
		rmdir($dir);
	}
}

The code where it goes wrong (bit messy).
PHP:
	if($err == 1)
	{
		echo 'ERROR: File Mismatch. Checksum failed on file \''.$mdlname.'\' ('.$checksum.' -> '.$checksum_cval.'). Check that file is correct.';
		decompile_log($udir, 'decompile.php ERROR: Checksum failed on file \''.$mdlname.'\' ('.$checksum.' -> '.$checksum_cval.').');
	}
	else
	{
		vmd_mkdir('models/'.$checksum);
		$bin_mdl = vmd_fread('temp/'.$udir.'/'.$mdlname.'.mdl');
		$mdl = vmd_qc($bin_mdl, $mdlname);
		$vvd = vmd_verts($bin_vvd); //Extract vertex pool.
		$vtx = vmd_tris($bin_vtx); //Assemble faces in correct draw order as a triangle strip.
		if($phy)
		{
			$phy = vmd_phy($bin_phy);
			$mdl[0] .= $phy[0];
		}
		$meshnames = vmd_smd($vvd, $vtx, $mdl[4], $checksum, $mdl[1], $mdlname);
		vmd_rmdir('temp/'.$udir);
		$dfile = new ZipArchive;
		if($dfile->open('download/'.$checksum.'/'.$mdlname.'.zip', ZipArchive::CREATE) === TRUE)
		{
			$dfile->addFromString($mdlname.'.qc', $mdl[0]);
			foreach($meshnames as $meshname)
			{
				$dfile->addFile('models/'.$checksum.'/'.$meshname, $meshname);
			}
			$dfile->close();
			echo '<a href="download/'.$checksum.'/'.$mdlname.'.zip" target="_blank">Download</a>';
			decompile_log($udir, 'decompile.php SUCCESS: User decompiled a new model (download/'.$checksum.'/'.$mdlname.'.zip).');
		}
		else
		{
			echo 'ERROR: Could not create file archive for download.';
			decompile_log($udir, 'decompile.php ERROR: Could not create file archive for download (download/'.$checksum.'/'.$mdlname.'.zip).');
		}
		vmd_rmdir('models/'.$checksum);
	}

The error
Code:
Warning: unlink(models/09006a36/.nfs00000000008b48ca00000746) [function.unlink]: Device or resource busy in lib_vmd.php on line 29
Warning: rmdir(models/09006a36) [function.rmdir]: Directory not empty in lib_vmd.php on line 33
 
OK, seems that it was because I forgot to create the 'download' folder before removing the directory but I still don't see the link between that and this. :/

Is it because of the ZipArchive class?
 
from php.net

Beware: calling $zip->addFile() on a file that doesn't exist will succeed and return TRUE, delaying the failure until you make the final $zip->close() call, which will return FALSE and potentially leave you scratching your head.

If you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist.

It's also a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.

So my first thing is first, check "$dfile->close();" is returning true.
 
Back
Top Bottom