/** * Copy a file to a new location. * * @param string $path * @param string $target * @return bool */ public function copy($path, $target) { return copy($path, $target); }// wrap a copy function /** * Extract the file name from a file path. * * @param string $path * @return string */ public function name($path) { return pathinfo($path, PATHINFO_FILENAME);// get path file name }// get the file name by file name path use this function by pathinfo /** * Extract the file extension from a file path. * * @param string $path * @return string */ public function extension($path) { return pathinfo($path, PATHINFO_EXTENSION); }// get the file extension, by path extension /** * Get the file type of a given file. * * @param string $path * @return string */ public function type($path) { return filetype($path); }// type is a wrap about this file type /** * Get the mime-type of a given file. * * @param string $path * @return string|false */ public function mimeType($path) { return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); }// mimeType is what, don‘t know /** * Get the file size of a given file. * * @param string $path * @return int */ public function size($path) { return filesize($path);// get file size use filesize function or method }// Get the file size of a given file /** * Get the file‘s last modification time. * * @param string $path * @return int */ public function lastModified($path) { return filemtime($path);// use file make time }// get the file last modification time /** * Determine if the given path is a directory. * * @param string $directory * @return bool */ public function isDirectory($directory) { return is_dir($directory); }// determine is a dir /** * Determine if the given path is writable. * * @param string $path * @return bool */ public function isWritable($path) { return is_writable($path); }//determine it is writable /** * Determine if the given path is a file. * * @param string $file * @return bool */ public function isFile($file) { return is_file($file); }// determine it is a file /** * Find path names matching a given pattern. * * @param string $pattern * @param int $flags * @return array */ public function glob($pattern, $flags = 0) { return glob($pattern, $flags); }// a matching about pattern /** * Get an array of all files in a directory. * * @param string $directory * @return array */ public function files($directory) { $glob = glob($directory.‘/*‘); if ($glob === false) { return []; }// first get all // To get the appropriate files, we‘ll simply glob the directory and filter // out any "files" that are not truly files so we do not end up with any // directories in our list, but only true files within the directory. return array_filter($glob, function ($file) { return filetype($file) == ‘file‘; });// second get the right thing } /** * Get all of the files from the given directory (recursive). * * @param string $directory * @return array */ public function allFiles($directory) { return iterator_to_array(Finder::create()->files()->in($directory), false); }// get all files form a given directory use a recursive method /** * Get all of the directories within a given directory. * * @param string $directory * @return array */ public function directories($directory) { $directories = []; foreach (Finder::create()->in($directory)->directories()->depth(0) as $dir) { $directories[] = $dir->getPathname(); } return $directories; }// get all directories /** * Create a directory. * * @param string $path * @param int $mode * @param bool $recursive * @param bool $force * @return bool */ public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false) { if ($force) { return @mkdir($path, $mode, $recursive); }// force make directory return mkdir($path, $mode, $recursive); }// normal dir /** * Copy a directory from one location to another. * * @param string $directory * @param string $destination * @param int $options * @return bool */ public function copyDirectory($directory, $destination, $options = null) { if (! $this->isDirectory($directory)) { return false; }// determine directory $options = $options ?: FilesystemIterator::SKIP_DOTS; // If the destination directory does not actually exist, we will go ahead and // create it recursively, which just gets the destination prepared to copy // the files over. Once we make the directory we‘ll proceed the copying. if (! $this->isDirectory($destination)) { $this->makeDirectory($destination, 0777, true); }// if not exists we will make it $items = new FilesystemIterator($directory, $options);// get items foreach ($items as $item) {// check // As we spin through items, we will check to see if the current file is actually // a directory or a file. When it is actually a directory we will need to call // back into this function recursively to keep copying these nested folders. $target = $destination.‘/‘.$item->getBasename(); if ($item->isDir()) { $path = $item->getPathname(); if (! $this->copyDirectory($path, $target, $options)) { return false; } } // If the current items is just a regular file, we will just copy this to the new // location and keep looping. If for some reason the copy fails we‘ll bail out // and return false, so the developer is aware that the copy process failed. else { if (! $this->copy($item->getPathname(), $target)) { return false; } }// aware like know } return true; }// copy directory /** * Recursively delete a directory. * * The directory itself may be optionally preserved. * * @param string $directory * @param bool $preserve * @return bool */ public function deleteDirectory($directory, $preserve = false) { if (! $this->isDirectory($directory)) { return false; } $items = new FilesystemIterator($directory); foreach ($items as $item) { // If the item is a directory, we can just recurse into the function and // delete that sub-directory otherwise we‘ll just delete the file and // keep iterating through each file until the directory is cleaned. if ($item->isDir() && ! $item->isLink()) { $this->deleteDirectory($item->getPathname()); } // If the item is just a file, we can go ahead and delete it since we‘re // just looping through and waxing all of the files in this directory // and calling directories recursively, so we delete the real path. else { $this->delete($item->getPathname()); } } if (! $preserve) { @rmdir($directory);// force delete } return true; }// delete all /** * Empty the specified directory of all files and folders. * * @param string $directory * @return bool */ public function cleanDirectory($directory) { return $this->deleteDirectory($directory, true); }// a wrap function }
时间: 2024-10-26 03:59:57