Upload file in laravel

末鹿安然 提交于 2021-02-10 06:20:08

问题


I guys I read the documentation for do a upload file in laravel... But I don't understand more it (I'm beginner) It's my image.blade.php

{{Form::open(['url'=>'administrator/store ', 'files' => true])}} 
{!!Form::file('image') !! } 
{!! Form::submit('next')!!} 
{{Form::close()}} 

Administrator Controller

use Storage; 
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 

public function store(Request $request) {
Storage::put($request->image, 'test') ;
} 

I don't understand what I will put into the function..... Help me pls... Greetings!


回答1:


\Illuminate\Http\Request::file() is what you have when you're uploading files.

This is just a instance of \Symfony\Component\HttpFoundation\File\UploadedFile class so you can move file to destination/storage what you want, something like that:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainController extends Controller
{
    public function upload(Request $request)
    {
        /**
         * @var Symfony\Component\HttpFoundation\File\UploadedFile
         */
        $uploadedFile = $request->file('image'); 

        if ($uploadedFile->isValid()) {
            $uploadedFile->move(destinationPath, $fileName);
        }
    }

}

Aso, you've been used \Illuminate\Filesystem\Filesystem::put() in a wrong way. Below is like that method is implemented:

/**
     * Write the contents of a file.
     *
     * @param  string  $path
     * @param  string  $contents
     * @param  bool  $lock
     * @return int
     */
    public function put($path, $contents, $lock = false)
    {
        return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
    }


来源:https://stackoverflow.com/questions/39239049/upload-file-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!