Git Product home page Git Product logo

laravel-uploader's Introduction

Laravel Uploader

🌴 An upload component for Laravel.

Sponsor me

Installing

  1. Install package:

    $ composer require overtrue/laravel-uploader -vvv

    and publish the assets using command:

    $ php artisan vendor:publish --provider=Overtrue\\LaravelUploader\\UploadServiceProvider
  2. Routing

    You can register routes in routes/web.php or other routes file:

    \LaravelUploader::routes();
    
    // custom
    \LaravelUploader::routes([
       'as' => 'files.upload', 
       'middleware' => ['auth'],
       //...
    ]); 

Usage

Custom controller

If you want to handle file upload, you can do it as simple as:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Overtrue\LaravelUploader\StrategyResolver;

class MyUploadController extends BaseController
{
    public function upload(Request $request)
    {
        return StrategyResolver::resolveFromRequest($request, $request->get('strategy', 'default'))->upload();
    }
}

Custom Response

If you want update the response, you can get key information from the return value object and return a new response:

    public function upload(Request $request)
    {
        $response = StrategyResolver::resolveFromRequest($request, $request->get('strategy', 'default'))->upload();
        
        return response()->json([
            'status' => 'success',
            'url' => $response->url,
            'origin_name' => $response->originalName,
            //...
        ]);
    }

You can get all these public properties:

int $size;
string $path;
string $mime;
string $url;
string $relativeUrl;
string $filename;
string $originalName;
\Illuminate\Http\UploadedFile   $file;
\Overtrue\LaravelUploader\Strategy $strategy;

Recommend clients

❤️ Sponsor me

Sponsor me

如果你喜欢我的项目并想支持它,点击这里 ❤️

Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

laravel-uploader's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar jcc avatar linhaijian99 avatar osindex avatar overtrue avatar summerblue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-uploader's Issues

variable URL not defined

hi, i get this code in my page
data-item-template="<img src='{URL}' />"

and my uploaded images in folder storage/app/public/uploads but my img url like this

<img src="http://localhost/storage/uploads/oYtVx6EDCP8NWUyY9JKfQK4JjPnfwdE7v0qWDtGk.jpg">
how to fix that

关于设定最大上传数的一个问题

诶一个问题:
当设定最大上传数的时候,比如设定max为3,但是依旧可以上传超过三张图片。详细如下:
image
当已经到了设定最大数的时候,那个选择图片的按钮还在,这时候如果继续选择图片,前端会直接报错:
image
但是实际上图片也已经传上去了:
image
所以建议前端做一个判断,达到最大数的时候就不显示按钮了。

第二个问题:
当图片上传出错的时候,没有做容错机制,如下:
image
前端报错:
image
这时候,就无法继续传图片了。

我也不知道为什么那张图片永远上传失败,我也很无奈啊~

Laravel 5.2 及以下版本不能正常使用

5.2 版本下,发现两个问题:

一、对模板扩展的参数解析问题
src/LaravelUploader/UploadServiceProvider.php 117 行解析参数时,$expression 会有括号存在,
比如 @uploader('images') , $expression 在 5.2 版本下会解析为 ('images') ,而 5.3 及以上版本会解析为 'images'

二、上传文件组件缺少 storeAs 方法
Illuminate/Http/UploadedFile.php 这个文件的 storeAs 方法只在 5.3 及以上版本才有

关于disk设定的一个问题

在这段代码中:

return [
                'success' => true,
                'filename' => $hashName,
                'original_name' => $file->getClientOriginalName(),
                'mime' => $mime,
                'size' => $file->getClientSize(),
                'relative_url' => $path,
                'url' => Storage::url($path),
                'dataURL' => $this->getDataUrl($mime, $this->filesystem->disk($disk)->get($path)),
        ];

Storage并没有选定磁盘,而是使用了默认的。假如我在配置文件中设定的disk是七牛的话,文件可以上传成功,但是前台却无法正常显示。所以可以修改一下:

return [
                'success' => true,
                'filename' => $hashName,
                'original_name' => $file->getClientOriginalName(),
                'mime' => $mime,
                'size' => $file->getClientSize(),
                'relative_url' => $path,
                'url' => Storage::disk($disk)->url($path),
                'dataURL' => $this->getDataUrl($mime, $this->filesystem->disk($disk)->get($path)),
        ];

laravel 5.4报错

Undefined index: filters (View: /home/vagrant/Code/wxapp/resources/views/vendor/uploader/images.blade.php) (View: /home/vagrant/Code/wxapp/resources/views/vendor/uploader/images.blade.php)

请问对各类上传漏洞会有计划吗?

首先非常感谢你的easywechat,为了表达支持,早已经购买了教程,虽然文档已经很详细了!😂

PHP有很多上传的漏洞,比如任意文件上传漏洞,抓包修改MIME绕开验证,虽然大部分的路径都屏蔽了对应的执行权限,但是比如JS代码这些就很难处理,一个图片 include 其他txt这种也不是没有可能性,所以在上传这块一直不知道如何处理,才能杜绝此类漏洞或者说潜在威胁。

可否增加用户自定义的事件回调钩子呢

目前原生的plupload事件好像都被默认固定实现了,
可否在事件回调中增加用户自定义的钩子函数呢,
这样可以比如:一张图片完成之后,可以将文件线上路径赋值给某表单数组,
再如,点击删除按钮后,可以将文件线上路径从某表单数组删除,
这样子

如何设置默认文件?

预期动作

 @uploader('file', ['name' => 'image','data'=>$directory->image])

或某种其他设置可以正常输出结果。

当前动作

 @uploader('file', ['name' => 'image','data'=>$directory->image])

这样的设置没有任何效果。

可能的解决方案

内容

无法设置默认的文件。

图示

snip20170820_2
snip20170820_4

环境

  • 使用的版本:"laravel/framework": "5.5.*","overtue/laravel-uploader": "^0.0.7"
  • 环境名和版本信息: PHP 7.1 on Nginx 1.12.0_1
  • 操作系统类型和版本: macOS 10.12.6

相关代码

// @uploader('file', ['strategy' => 'avatar', 'data' => [$product->images]])

<div class="file-uploader file-uploader-file" id="{{ $id or uniqid('uploader_') }}" data-strategy="{{ $strategy }}" data-items='{!! !empty($file) ? '["'.asset($file).'"]' : '[]' !!}' data-item-template="<div class='file-uploader-item-actions'><a href='{URL}' target='_blank'>查看</a></div>" data-form-name="{{ $name }}" data-filters="{{ json_encode(uploader_strategy($strategy)['filters']) }}">

关于存储图片的路径的问题

我看到input中赋值的是relativeUrl,是以storage开头的路径。
我想问问,如果用七牛,也是直接存储这个?那么显示的时候再重新组合成url?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.