search
HomeBackend DevelopmentPHP Tutoriallaravel 基础教程 -- 文件系统

文件系统/云存储

简介

laravel 提供了一个强大的文件系统的抽象,这得益于 Frank de Jonge 所开发的 Flyststem PHP 包。laravel 的文件系统提供了对一些存储驱动的支持,它们包括本地文件系统,Amazon S3,Rackspace 云存储。更为奇妙的是,它可以通过存储配置选项来切换这些存储系统,因为 laravel 对它们提供了统一的 API 接口。

配置

文件系统的配置选项存储在 config/filesystems.php 文件中。在这个文件中你可以对所有的磁盘进行配置。每个磁盘选项包含着其所使用的存储驱动及其存储位置。对于 laravel 默认支持的存储驱动,在这个文件中都有相应的配置示例。所以,你可以简单的在这个文件中修改配置选项就可以使用其强大的功能。

当然,你可以配置多个磁盘,甚至可以使多个磁盘使用相同的驱动。

公共磁盘

public 磁盘意味着其可以被公开的访问。默认的 public 磁盘使用的是 local 驱动并且其存储的文件位置是在 storage/app/public 目录。如果你想要这个目录下的文件可以在 web 中进行访问,你需要创建一个 public/storage 到 storage/app/public 的符号链接。这个约定可以使公开访问的文件保持存放在同一个目录中并且可以在使用像 Envoyer 这种无痛持续部署系统时可以方便的共享整个部署过程。

当然,一旦文件被存储并且建立了符号链接。你就可以通过 asset 帮助方法来生成文件的 URL:

echo asset('storage/file.txt')

本地驱动

当使用 local 驱动时,你需要知道的是所有的文件操作都是相对于配置文件中的 root 选项所定义的目录。默认的这个值设置的是 storage/app 目录。因此,下面的方法将文件存储到 storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

其他驱动先决条件

在使用 S3 或者 Rackspace 驱动之前,你需要先通过 Composer 来安装适当的包文件:

  • Amazon S3: league/flysystem-aws-s3-v3 ~1.0
  • Rackspace: league/flysystem-rackspace ~1.0

FTP 驱动配置

laravel 的文件系统可以很好的支持 FTP 的集成,但是在默认的配置文件中并没有给出示例。如果你需要配置 FTP 的文件系统,你可以使用下面的配置示例:

'ftp' => [  'dirver' => 'ftp',  'host' => 'ftp.example.com',  'username' => 'your-username',  'password' => 'your-password',  // Optional FTP Settings...  // 'port' => 21,  // 'root' => '',  // 'passive' => true,  // 'ssl' => true,  // 'timeout' => 30,],

Rackspace 驱动配置

laravel 的文件系统可以很好的支持 Rackspace 的集成,但是在默认的配置文件中并没有给出示例。如果你需要配置 Rackspace 文件系统,你可以使用下面的示例:

'rackspace' => [  'driver' => 'rackspace',  'username' => 'your-username',  'key' => 'your-key',  'container' => 'your-container',  'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',  'region' => 'IAD',  'url_type' => 'publicURL',],

基础用法

获取磁盘实例

Storage 假面可以用来和你所配置的磁盘进行交互。比如,你可以使用它的 put 方法来将用户的头像图片存储到默认的磁盘。如果你在调用该方法的时候没有在其之前使用 disk 方法的话,那么该方法会自动的将头像传递到默认的磁盘:

<?phpnamespace App\Http\Controllers;use Storage;use Illuminate\Http\Request;use App\Http\Controllers\Controller;class UserController extends Controller{  /**   * Update teh avatar for the given user.   *   * @param Request $request   * @param int $id   * @return Response   */   public function updateAvatar(Request $request, $id)   {     $user = User::findOrFail($id);     Storage::put(        'avatars/'.$user->id,        file_get_contents($request->file('avatar')->getRealPath())      );   }}

当你使用多个磁盘时,你可以使用 Storage 假面的 disk 方法来指定访问的磁盘。当然,你可以使用链式的方法来进行持续的操作:

$disk = Storage::disk('s3');$contents = Storage::disk('local')->get('file.jpg');

检索文件

get 方法可以用来从所给定的文件中检索出其内容。该方法会返回文件的原始字符串内容:

$contents = Storage::get('file.jpg');

exists 方法可以用来判断所给定的文件是否存在于磁盘中:

$exists = Storage::disk('s3')->exists('file.jpg');

文件 URLs

当使用 local 或者 s3 驱动时,你可以使用 url 方法来获得给定文件的 URL。如果你使用的是 local 驱动,那它只会简单的在给定的路径前增加 /storage 前缀以返回相对的文件路径。如果你使用的是 s3 驱动,将会返回完整的远端 RUL:

$url = Storage::url('file1.jpg');

注意:当使用的是 local 驱动时,一定要确保创建了 public/storage 到 storage/app/public 的符号链接。

文件元信息

size 方法可以用来获取给定文件的字节大小:

$size = Storage::size('file1.jpg');

lastModified 方法会返回给定文件的最后修改时间,它使用的 是 UNIX 时间戳:

$time = Storage::lastModified('file1.jpg');

存储文件

put 方法可以用来将文件存储到磁盘。你可以传递一个 PHP 的 resource 到 put 方法,那么它会使用文件系统的底层流支持。在与大型文件交互时,推荐使用文件流:

Storage::put('file.jpg', $contents);Storage::put('file.jpg', $resource);

copy 方法可以用来复制磁盘中已存在的文件到新的位置:

Storage::copy('old/file1.jpg', 'new/file1.jpg');

move 方法可以被用对磁盘中已存在的文件进行名称的修改或移动到新的位置:

Storage:move('old,file1.jpg', 'new/file1.jpg');

前置或追加内容到文件

prepend 和 append 方法允许你轻松的往文件的起始或结束位置加入内容:

Storage::prepend('file.log', 'Prepended Text');Storage::append('file.log', 'Appended Text');

文件可见性

你可以通过使用 getVisibility 和 setVisibility 方法来进行文件可见性的检索和设置。可见性是跨平台的文件权限的抽象:

Storage::getVisibility('file.jpg');Storage::setVisibility('file.jpg', 'public');

另外,你可以在使用 put 方法的同时设置文件的可见性。有效的可见性值是 public 和 private:

Storage::put('file.jpg', $contents, 'public');

删除文件

delete 方法可以接受一个文件名或者文件名所组成的数组,它将从磁盘中删除相应的文件:

Storage::delete('file.jpg');Storage::delete(['file1.jpg', 'file2.jpg']);

目录

从目录中获取所有文件

files 方法会返回所给定目录中所有的文件所组成的数组。如果你想要在检索到的文件中包含所给定目录的子目录。那么你需要使用 allFiles 方法:

$file = Storage::files($directory);$files = Storage::allFiles($directory);

从给定的目录中获取所有的目录

directories 方法可以返回所给定目录下的所有子目录所组成的数组。另外你可以使用 allDirectories 方法递归检索子目录:

$directories = Storage::directories($directory);// Recursive...$directories = Storage::allDirectories($directory);

创建目录

makeDirectory 方法将创建给定的目录,包括其所需要的子目录:

Storage::makeDirectory($directory);

删除一个目录

最后,deleteDirectory 方法可以用来删除一个目录,并且其删除磁盘中该目录下所有的文件:

Storage::deleteDirectory($directory);

自定义文件系统

laravel 的文件系统对几种常见的存储系统提供了开箱即用的支持。事实上,文件系统并没有限制你只使用所提供的这些。你可以自己创建一个适配器来构建一个自定义的驱动去支持你所期望使用的文件存储系统。

你需要创建一个服务提供者来进行自定义文件存储系统的构建。比如 DropboxServiceProvider。在提供者的 boot 方法中,你需要使用 Storage 假面的 extend 方法来定义你自己的驱动:

<?phpnamespace App\Providers;use Storage;use League\Flysystem\Filesystem;use Dropbox\Client as DropboxClient;use Illuminate\Support\ServiceProvider;use League\Flysystem\Dropbox\DropboxAdapter;class DropboxServiceProvider extends ServiceProvider{  /**   * Perform post-registration booting of services.   *   * @return void   */   public function boot()   {     Storage::extend('dropbox', function ($app, $config) {       $client = new DropboxClient(        $config['accessToken'], $config['clientIdentifier']       );             return new Filesystem(new DropboxAdapter($client));     });   }   /**    * Register bindings in the container.    *    * @return void    */    public function register()    {      //    }}

extend 方法中的第一个参数应该是驱动的名称,第二个参数是一个闭包,闭包接受 $app 和 $config 变量。被解析的闭包必须返回一个 League\Flysystem\Filesystem 的实例。$config 变量包含了 config/filesystems.php 文件中指定磁盘的值。

一旦你创建了服务提供者并且注册了这个扩展,你就可以在 config/filesystem.php 配置文件中使用 dropbox 驱动了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools