undjike / nmfs-uploads
此包允许您将任意大小的文件上传到您的PHP服务器
This package is auto-updated.
Last update: 2024-09-21 16:21:19 UTC
README
介绍
Nmfs-uploads包允许您将任意大小的文件上传到您的PHP服务器。
您当然知道默认的PHP服务器配置将文件上传大小限制在2 Mb最大。使用此包,您无需更改php.ini或其他默认配置。
Nmfs-uploads包为您提供了一些工具并实现了分块上传。这意味着无论要上传的文件有多大,服务器都会将其分成小于2 Mb的小部分,并在最后自动合并这些部分。
安装
使用以下命令通过composer安装此包
composer require undjike/nmfs-uploads
更新composer后,如果您使用的是5.5以下的Laravel版本,请将服务提供者添加到config/app.php中的providers数组中。
Undjike\NmfsUploads\NmfsUploadsServiceProvider::class
Laravel 5.5或更高版本使用包自动发现,因此不需要您手动添加ServiceProvider。
通过composer安装包后,您需要发布包的配置。使用以下artisan命令
php artisan vendor:publish --provider="Undjike\NmfsUploads\NmfsUploadsServiceProvider"
这将发布使包正常工作所需的所有必需文件和一些用于演示如何使用此包的文件。
现在,您只需访问您的服务器页面/uploads
(使用您的Laravel开发服务器:localhost:8000/uploads
),就可以看到这个功能是如何工作的。
用法
配置发布将为您生成
- 项目配置目录中的配置文件,
config/nmfs-uploads.php
。
<?php /** * * * * # nmfs-uploads * * * * @author Ulrich Pascal Ndjike Zoa <ndjikezoaulrich@gmail.com> * * @copyright 2020 Ulrich Pascal Ndjike Zoa / RYS Studio * * @license https://open-source.org.cn/licenses/mit-license.php MIT * * @link https://github.com/undjike/nmfs-uploads * */ return [ // The directory to store the uploaded files to 'upload_dir' => public_path() . '/temps/', // The URL to which the upload request is sent // When null, the request will be sent to the current url // with POST method instead 'upload_url' => null, // The input file name that is handling the file 'param_name' => 'file', // When uploading a large file, the size of chunks in // which the file will be divided // Default : 2 Mo, corresponding to the default limit // of uploadable file size to PHP server 'readfile_chunk_size' => 2 * 1024 * 1024, // The accepted file types // Default accepted files : pdf, mp4, flv, avi, mov, wmv, webm // For images you can use : '/\.(gif|jpe?g|png)$/i' 'accept_file_types' => '/\.(pdf|mp4|flv|avi|mov|wmv|webm)$/i', // The maximum uploadable file size // When mull, no limit 'max_file_size' => 550 * 1024 * 1024, //550 Mb // The minimum uploadable file size // Default 1, the uploaded file cannot be lest than 1 byte 'min_file_size' => 1, // The maximum number of files for the upload directory 'max_number_of_files' => null, // The HTTP method to use for DELETE request // Set the following option to 'POST', if your // server does not support DELETE requests 'delete_type' => 'DELETE', // CORS configurations 'access_control_allow_origin' => '*', 'access_control_allow_credentials' => false, // Methods to allow from requests 'access_control_allow_methods' => array('OPTIONS', 'HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE'), // Headers to allow from requests // NOTICE : DON'T REMOVE EXISTING ALLOWED HEADERS HERE // You are free to add some 'access_control_allow_headers' => array('Content-Type', 'Content-Range', 'Content-Disposition') ];
- 一个示例控制器
app/Http/Controllers/NmfsUploadsController.php
,其中包含两个方法index
和perform
。
<?php /** * * * * # nmfs-uploads * * * * @author Ulrich Pascal Ndjike Zoa <ndjikezoaulrich@gmail.com> * * @copyright 2020 Ulrich Pascal Ndjike Zoa / RYS Studio * * @license https://open-source.org.cn/licenses/mit-license.php MIT * * @link https://github.com/undjike/nmfs-uploads * */ namespace App\Http\Controllers; use Illuminate\Contracts\View\Factory; use Illuminate\View\View; use Undjike\NmfsUploads\Uploader; class NmfsUploadsController extends Controller { /** * Show the upload page * * @return Factory|View */ public function index() { return view('nmfs-uploads'); } /** * Perform file upload * * @return void */ public function perform() { Uploader::receive(function ($response) { // TODO: Perform some actions on upload done }); } }
- 一个视图
resources/views/nmfs-uploads.blade.php
,这是一个用于执行上传的示例页面。
该视图使用dropzone让您只需拖放文件即可上传到您的服务器。如上所示,控制器中的index
方法简单地返回此视图。
- 如您所猜测的,已添加两个
routes
到您的web.php
。
第一个,名为upload
,使用GET
方法用于显示上传页面,第二个使用POST
,用于上传请求。
// Routes generated for demonstration uses of nmfs-uploads package // Feel free to delete them when you are done Route::get('uploads', 'NmfsUploadsController@index')->name('upload'); Route::post('uploads', 'NmfsUploadsController@perform')->name('perform.upload');
- 最后,该包在
public/vendor/nmfs-uploads
中提供了一些资产。
your-project/
└───public/
└───vendor/
└───nmfs-uploads/
└───blueimp-canvas-to-blob/
└───blueimp-file-upload/
└───blueimp-load-image/
└───blueimp-tmpl/
└───bootstrap/
└───fontawesome-free/
└───jquery/
└───nmfs-client.js
└───style.css
这些资产中的大部分用于美观地渲染您的上传页面。
我们需要提供bootstrap
和jQuery
以确保它们对包可用。如果您已经拥有它们,请随意删除这些目录并替换上传视图中链接。
所有blueimp
目录都用于执行包的主要功能。如果您想了解更多关于包的信息,请访问blueimp。
理解
控制器中的 perform
方法中的 receive
闭包允许你在上传成功完成后执行一些操作。默认情况下,上传文件存储在公共目录 temps
中。您可以自由地更改这个设置。名称 temps
只是为了说明通常会在那里获取上传文件并将其移动到自定义库中。个人来说,我们使用过 spatie/laravel-medialibrary 做过几次这样的操作。
当然,您一定已经意识到,在配置文件中的 URL 默认设置为 null
。这是故意的。为了允许您以非常轻量级的方式使用此包,当上传 URL 设置为 null
时,包将使用当前 URL 来提交上传。
当使用多个上传页面时,这非常有用。有意义的说法是,显示上传页面的 URL 与文件将被提交的 URL 相同。
// The URL to which the upload request is sent // When null, the request will be sent to the current url // with POST method instead 'upload_url' => null,
请注意,此包的客户端是用 jQuery 编写的,因此如果您对页面设计不满意或想要自定义某些方面,请编辑 public/vendor/nmfs-uploads/nmfs-client.js
。
注意
一些配置和限制条款可以在客户端和服务器端声明。当与具有不同访问级别的多个客户端一起工作时,这可能很有用。
对于此包的任何问题或问题,请给我发消息,我将非常乐意解决问题。
许可证
Nmfs-uploads 在 MIT 许可证 下分发。