/** * Deletes a directory and all its subdirectories and files. * * @param string $path Full or relative path to directory. * * @throws \InvalidArgumentException If path is empty or is not a directory. * @uses File::getFullPath To format path to file. * @uses self::delete To recursively delete subdirectories. * @uses File::delete To delete files in directories. * * @return boolean Result of the operation. */ public static function delete($path) { $fullPath = File::getFullPath($path); if (empty($path) || !is_dir($fullPath)) { throw new \InvalidArgumentException('Path is empty or is not a directory.'); } /* Open a directory handle. */ $handle = opendir($fullPath); /* Read entries from the directory handle. */ while (($entry = readdir($handle)) !== false) { /* Skip directory handles for current and previous directories. */ if ($entry == '.' || $entry == '..') { continue; } /* Check whether the current entry is a directory and is not a symbolic link */ if (is_dir($fullPath . DIRECTORY_SEPARATOR . $entry) && !is_link($fullPath)) { self::delete($fullPath . DIRECTORY_SEPARATOR . $entry); } else { File::delete($fullPath . DIRECTORY_SEPARATOR . $entry); } } /* Close directory handle */ closedir($handle); $deleted = rmdir($fullPath); return $deleted; }
/** * Deletes all related file to an attachment. * * @param Base\Model $resource Currently processed resource. * @param string $attachment Attachment file name. * @param string $name Name of the file. * * @access private * @static * * @return void */ private static function deleteThumbnails(Base\Model $resource, $attachment, $name) { $_storage_path = $resource->attachmentsStoragePath($attachment); foreach (self::$attachments[$attachment]['thumbnails'] as $thumbnail) { try { if (file_exists($_storage_path . "{$thumbnail['size']}-{$name}")) { Helpers\File::delete($_storage_path . "{$thumbnail['size']}-{$name}"); } } catch (\Exception $e) { var_log($e->getMessage()); } } }
/** * @covers Core\Helpers\File::delete */ public function testDeletingFile() { $this->assertTrue(File::delete($this->baseName)); }
/** * Removes a key-value pair. * * @param string $key Cache key. * * @return boolean */ public function remove($key) { $file = self::storagePath() . self::generateName($key); try { return File::delete($file); } catch (\Exception $e) { var_log($e->getMessage()); return false; } }