FriendsOfCake/cakephp-upload

Error when I delete two or more files at the same folder

IsaacAnselmo opened this issue · 1 comments

When I need to delete two or more files at the same folder , I got error when trying to delete the folder.

RecursiveDirectoryIterator::__construct(\app\webroot\files\cfdi\7,\pagos\app\webroot\files\cfdi\7): Acceso denegado. (code: 5)

After tracing inside UploadBehavior I found the error is caused by the array in foldersToRemove, because contain all the id to delete and the PlugIn try to delete a folder doesn'n exist.

`

    public function deleteFolder(Model $model, $path) {
	if (!isset($this->__foldersToRemove[$model->alias])) {
		return false;
	}
	
	$this->__foldersToRemove[$model->alias]=array_unique($this->__foldersToRemove[$model->alias]);		

	$folders = $this->__foldersToRemove[$model->alias];
	foreach ($folders as $folder) {
		if (strlen((string)$folder) === 0) {
			continue;
		}

		$dir = $path . $folder;
		$it = new RecursiveDirectoryIterator($dir);
		$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
		foreach ($files as $file) {
			if ($file->getFilename() === '.' || $file->getFilename() === '..') {
				continue;
			}

			if ($file->isDir()) {
				$this->rmdir($file->getRealPath());
			} else {
				$this->unlink($file->getRealPath());
			}
		}
		$this->rmdir($dir);
	}

	unset($this->__foldersToRemove[$model->alias]);

	return true;
}

`

I put this issue in the pull requestr #551