Files can't be upload to directory and give back error
js and css from dropzone is included in head and footer properly
Balde code::
@include('Templates.head')
<body style="background: #283747;">
@include('Menu.menu')
@if (Session::has('user') && Session::has('privlages') && Session::get('privlages') >= '1')
<div class="container">
<div class="row">
<form action="/gallery_add_image" class="dropzone" style="margin-left: 10%; margin-right: 10%;">
@csrf
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
</div>
</div>
@else
<script>window.location = "/";</script>
@endif
@include('Templates.footer')
Controller code::
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class GalerryController extends Controller
{
public function add_image(Request $add_image){
$image = $add_image->file('file');
$imageName = time(). '.' . $add_image->extension();
$image->move(public_path('gallery'), $imageName);
return response()->json(['success' => $imageName]);
}
}
web(routing) code::
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GalerryController;
Route::post('/gallery_add_image', [GalerryController::class, 'add_image']);
Server response code::
How can I fix this problem?
In your controller code $add_image
is a request object, which does not have an extension
method. To get the uploaded file extension you need to use:
class GalerryController extends Controller
{
public function add_image(Request $add_image) {
$add_image->validate([ 'file' => 'required|image' ]); // Good idea to validate
$image = $add_image->file('file');
$imageName = time(). '.' . $image->getClientOriginalExtension();
$image->move(public_path('gallery'), $imageName);
return response()->json(['success' => $imageName]);
}
}