在 Laravel 5 中集成七牛云存储实现云存储功能
本 扩展包 基于 https://github.com/qiniu/php-sdk 开发,是七牛云储存Laravel 5Storage版,通过本扩展包可以在Laravel 5中集成七牛云存储功能。
1、安装配置
使用之前,先通过Composer安装:
composer require zgldh/qiniu-laravel-storage
然后在 config/app.php 中注册服务提供者:
zgldh\QiniuStorage\QiniuFilesystemServiceProvider::class
接下来在 config/filesystems.php 里的 disks 中新增如下选项:
'disks' => [ ... , 'qiniu' => [ 'driver' => 'qiniu', 'domains' => [ 'default' => 'laravelacademy.com1.z0.glb.clouddn.com', //你的七牛域名 'https' => 'dn-laravelacademy.qbox.me', //你的HTTPS域名 'custom' => 'static.laravelacademy.org', //你的自定义域名 ], 'access_key'=> '', //AccessKey 'secret_key'=> '', //SecretKey 'bucket' => '', //Bucket名字 'notify_url'=> '', //持久化处理回调地址 ], ],
2、使用
第一种用法:
$disk = \Storage::disk('qiniu');$disk->exists('file.jpg'); //文件是否存在$disk->get('file.jpg'); //获取文件内容$disk->put('file.jpg',$contents); //上传文件$disk->prepend('file.log', 'Prepended Text'); //附加内容到文件开头$disk->append('file.log', 'Appended Text'); //附加内容到文件结尾$disk->delete('file.jpg'); //删除文件$disk->delete(['file1.jpg', 'file2.jpg']);$disk->copy('old/file1.jpg', 'new/file1.jpg'); //复制文件到新的路径$disk->move('old/file1.jpg', 'new/file1.jpg'); //移动文件到新的路径$size = $disk->size('file1.jpg'); //取得文件大小$time = $disk->lastModified('file1.jpg'); //取得最近修改时间 (UNIX)$files = $disk->files($directory); //取得目录下所有文件$files = $disk->allFiles($directory); //这个没实现。。。$directories = $disk->directories($directory); //这个也没实现。。。$directories = $disk->allDirectories($directory); //这个也没实现。。。$disk->makeDirectory($directory); //这个其实没有任何作用$disk->deleteDirectory($directory); //删除目录,包括目录下所有子文件子目录$disk->getDriver()->uploadToken('file.jpg'); //获取上传Token$disk->getDriver()->downloadUrl('file.jpg'); //获取下载地址$disk->getDriver()->downloadUrl('file.jpg', 'https'); //获取HTTPS下载地址$disk->getDriver()->privateDownloadUrl('file.jpg'); //获取私有bucket下载地址$disk->getDriver()->privateDownloadUrl('file.jpg', 'https');//获取私有bucket的HTTPS下载地址$disk->getDriver()->imageInfo('file.jpg'); //获取图片信息$disk->getDriver()->imageExif('file.jpg'); //获取图片EXIF信息$disk->getDriver()->imagePreviewUrl('file.jpg','imageView2/0/w/100/h/200'); //获取图片预览URL$disk->getDriver()->persistentFop('file.flv','avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240'); //执行持久化数据处理$disk->getDriver()->persistentFop('file.flv','fop','队列名'); //使用私有队列执行持久化数据处理$disk->getDriver()->persistentStatus($persistent_fop_id); //查看持久化数据处理的状态。
第二种用法 (就是省略了一个getDriver):
use zgldh\QiniuStorage\QiniuStorage;$disk = QiniuStorage::disk('qiniu');$disk->exists('file.jpg'); //文件是否存在$disk->get('file.jpg'); //获取文件内容$disk->put('file.jpg',$contents); //上传文件$disk->prepend('file.log', 'Prepended Text'); //附加内容到文件开头$disk->append('file.log', 'Appended Text'); //附加内容到文件结尾$disk->delete('file.jpg'); //删除文件$disk->delete(['file1.jpg', 'file2.jpg']);$disk->copy('old/file1.jpg', 'new/file1.jpg'); //复制文件到新的路径$disk->move('old/file1.jpg', 'new/file1.jpg'); //移动文件到新的路径$size = $disk->size('file1.jpg'); //取得文件大小$time = $disk->lastModified('file1.jpg'); //取得最近修改时间 (UNIX)$files = $disk->files($directory); //取得目录下所有文件$files = $disk->allFiles($directory); //这个没实现。。。$directories = $disk->directories($directory); //这个也没实现。。。$directories = $disk->allDirectories($directory); //这个也没实现。。。$disk->makeDirectory($directory); //这个其实没有任何作用$disk->deleteDirectory($directory); //删除目录,包括目录下所有子文件子目录$disk->uploadToken('file.jpg'); //获取上传Token$disk->downloadUrl('file.jpg'); //获取下载地址$disk->downloadUrl('file.jpg', 'https'); //获取HTTPS下载地址$disk->privateDownloadUrl('file.jpg'); //获取私有bucket下载地址$disk->privateDownloadUrl('file.jpg', 'https'); //获取私有bucket的HTTPS下载地址$disk->imageInfo('file.jpg'); //获取图片信息$disk->imageExif('file.jpg'); //获取图片EXIF信息$disk->imagePreviewUrl('file.jpg','imageView2/0/w/100/h/200'); //获取图片预览URL$disk->persistentFop('file.flv','avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240'); //执行持久化数据处理$disk->persistentFop('file.flv','fop','队列名'); //使用私有队列执行持久化数据处理$disk->persistentStatus($persistent_fop_id); //查看持久化数据处理的状态。
3、相关参考
本扩展包GitHub地址为 https://github.com/zgldh/qiniu-laravel-storage ,基于 https://github.com/qiniu/php-sdk 开发,更多详情请参考七牛官方PHP SDK使用指南: http://developer.qiniu.com/code/v7/sdk/php.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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.

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 have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

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.
