简体   繁体   中英

Laravel 8 UploadFile doesnt upload file to storage

I am trying to do the user account where the user can upload files that will be visible only for this user. I already have the form, but the form doesn't work correctly, in the./public/storage/images doesn't appear anything. I think that is because of access rights for the storage, but I don't know how to give access(i use windows).

I have already tried make changes in config\filesystem.php 'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],

],
'links' => [
    public_path('storage') => storage_path('public'),
],

];

Can you please help?

<form  action="{{url('image-upload')}}" enctype="multipart/form-data" method="post">
     @csrf
     <input type="file" name="file">
     <input type="submit">
</form>

**Controller**

 <?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ImageUploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View
     */
    public function imageUpload()
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function imageUploadPost(Request $request): RedirectResponse
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $imageName = time().'.'.$request->image->extension();

        $request->image->move(public_path('images'), $imageName);

        return back()
            ->with('success','You have successfully upload image.')
            ->with('image',$imageName);

    }
}

**Routes, web.php :**
Route::get('image-upload', 'ImageUploadController@imageUpaload')->name('image.upload');
    Route::post('image-upload', 'ImageUploadController@imageUploadPost')->name('image.upload.post');

Currently I am working with something like this so here is what works for me:

Firstly your config file looks good. Let's look at your Controller, so I think that you need to check if the file exists, something like this:

if ($request->file('image') != null) 
{
    $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

    $imageName = time().'.'.$request->image->extension();

    $request->image->move(public_path('images'), $imageName);

    return back()
         ->with('success','You have successfully upload image.')
         ->with('image',$imageName);
}

The rest of your code looks good and should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM