I'm uploading multiple images on server but instead of saving images in database and folder a 403 error occured. so please help me how to resolve it.
This is the Form & Controller code:
<form method="POST" action="{{ route('AddImages') }}" id="smart" enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" value="{{$id}}">
<div class="form-body">
<div class="form-row">
<div class="section col-6">
<label for="bname" class="field-label">Image</label>
<label for="bname" class="field prepend-icon">
<input type="file" name="image[]" class="gui-input" multiple required>
<span class="field-icon"><i class="fas fa-circle"></i></span>
</label>
</div><!-- end section -->
</div>
</div><!-- end .form-body section -->
<div class="form-footer">
<button type="submit" class="button btn-primary"> Save</button>
<button type="reset" class="button"> Cancel</button>
</div><!-- end .form-footer section -->
</form>
$images = $request->file('image');
$num = 0;
foreach ($images as $im) {
$files = $im->getClientOriginalExtension();
foreach ($request->image as $file) {
$image = new ClassImage();
$file_name = time() . rand(1, 999) . '.' . $file->getClientOriginalExtension();
$path = base_path('backend/images/class_images/');
$image->image = $file_name;
$image->class_id = $request->id;
try {
$file->move($path, $file_name);
$image->save();
$num++;
} catch (Exception $e) {
echo $e;
}
}
}
return redirect('ShowImages/'.$request->id);
I don't know what is the error in this code because on local server this code working fine but on server the problem 403 (Access denied) Occured.
Related
This is my blade code:
<form action="{{ route('picture.store') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="fotoRemolque" id="fotoRemolque"
placeholder="Tomar Foto de Remolque" class="inputsLogin"
multiple capture="camera" accept="image/*">
#error('fotoRemolque')
<small class=" text-danger">{{$message}}</small>
#enderror
<br>
<div class="col-12 text-center margen">
<h3 >Numero de Remolque:</h3>
<input type="text" name="remolque" id="result" class="inputRemolqueContenedor" readonly>
#error('remolque')
<small class=" text-danger"> {{$message}}</small>
#enderror
</div>
<input type="submit" value="EnviarImagen" id="enviarImg" class="btnEnviarImagen">
</form>
And here is my controller:
$request->validate([
'fotoRemolque' => 'required|image',
'remolque' => 'required|string'
]);
$remolque = $request->input('remolque');
$nombre = Auth::user()->username;
$terminal = Auth::user()->terminal;
$fecha = Carbon::now();
$pictureName = $nombre.$terminal.$remolque.$fecha.".png";
$images = $request->file('fotoRemolque')->store('public/imagesTrailers');
$url = Storage::url($images);
$url = Str::after( $url, 'http://localhost');
$picture = new Picture();
$picture->username = $nombre;
$picture->terminal = $terminal;
$picture->trailer = $remolque;
$picture->date = $fecha;
$picture->image_url = $url;
$picture->save();
The problem happens only on the mobile view, it doesn't upload the file, here is the problem:
I have the app in windows server 2012
I want to upload multiple images using 2 input file fields in Laravel and put that 2 files to DB with different attributes (imagepath1, imagepath2). If I try that code there both input & upload the same file like imagekitchen2 (there both change but imagepath1 become imagepath2 and imagepath2 still imagepath2).
Controller
public function store(Request $request)
{
$kitchens = new Kitchen();
$kitchens->title = $request->input('title-kitchen');
$kitchens->description = $request->input('description-kitchen');
if ($request->hasfile('imagekitchen1')) {
$file = $request->file('imagekitchen1');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/product/kitchen/', $filename);
$kitchens->imagepath1 = $filename;
} else {
$kitchens->imagepath1 = '';
}
$kitchens->save();
if ($request->hasfile('imagekitchen2')) {
$file = $request->file('imagekitchen2');
$extension = $file->getClientOriginalExtension();
$filename = time().'.'.$extension;
$file->move('uploads/product/kitchen/', $filename);
$kitchens->imagepath2 = $filename;
} else {
$kitchens->imagepath2 = '';
}
$kitchens->save();
}
View
<div class="card-body">
<div class="row">
<div class="col-md-6">
<form action="{{ route('addimagekitchen') }}" enctype="multipart/form-data" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label>Title</label>
<label>
<input type="text" name="title-kitchen" class="form-control">
</label>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Main image</label> <br/>
<input type="file" name="imagekitchen1" style="margin-left: 20px">
</div>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Second image</label> <br/>
<input type="file" name="imagekitchen2" style="margin-left: 20px">
</div>
</div>
<div class="input-group">
<div class="custom-file">
<label for="image" style="display: block">Third image</label> <br/>
<input type="file" name="imagekitchen[]" style="margin-left: 20px">
</div>
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" name="description-kitchen" id="description-kitchen"
rows="3"></textarea>
</div>
<button type="submit" class="btn btn-success"> Insert</button>
Cancel
</form>
</div>
</div>
</div>
I'm not completely sure what you mean, but probably you should change:
$kitchens->save();
if($request->hasfile('imagekitchen2')){
into
$kitchens->save();
$kitchens = new Kitchen();
if($request->hasfile('imagekitchen2')){
this way, you will create 2 records, otherwise you used same object and updated it after creation. Depending on your needs you might also want to add:
$kitchens->title = $request->input('title-kitchen');
$kitchens->description = $request->input('description-kitchen');
before:
if($request->hasfile('imagekitchen2')){
in case you want to save same title and description for both records.
Of course I'm not sure if you want to create records if there are no files - at the moment both will be saved in case no file uploaded.
hi I make a form to upload pics in Laravel but when I open the form then it shows error: Invalid argument supplied for foreach()
blade file:
<form method="POST" action="{{ route('admin.product.alternateimages') }}" enctype="multipart/form-data" class="add-new-post">
#csrf
<strong class="text-muted d-block mb-2 mt-5">Upload Product Image</strong>
<div class="input-group mb-3">
<div class="input-group input-group-seamless">
<input type="file" name="product_alt_img[]" class="form-control mb-2 btn btn-sm btn-outline-primary mr-1 #error('product_image') is-invalid #enderror" value="{{ old('product_image') }}" id="" placeholder=""> </div>
#error('product_image')
<div class="small text-danger">{{ $message }}</div>
#enderror
</div>
</form>
function:
public function alternateimages(Request $request)
{
$altimgs = new Product;
$altimgs->product_id = $request->product_id;
$files = $request->file('product_alt_img');
foreach ($files as $file) {
$images = $file->getClientOriginalName();
$file->move(public_path('images/backend_images/product_images'), $images);
$altimgs->product_alt_img = $images;
}
$altimgs->save();
return redirect()->back()->with('flash_message_success', 'Product Images has been added successfully');
}
You may have to change product_alt_img[] to product_alt_img if you are uploading a single file.
<input type="file" name="product_alt_img" class="form-control mb-2 btn btn-sm btn-outline-primary mr-1 #error('product_image') is-invalid #enderror" value="{{ old('product_image') }}" id="" placeholder="">
Else if wish to upload multiple file, you may have to modify your controller method.
public function alternateimages(Request $request)
{
$altimgs = new Product;
$altimgs->product_id = $request->product_id;
//fetch an array of product_alt_img
$files = $request->product_alt_img;
//go over each Illuminate\Http\UploadedFile instance
foreach($files as $file)
{
$image = $file->getClientOriginalName();
....
}
....
}
NB: A more eloquent solution is to have a separate table for storing products images
I have a table called Services, now this table has the following -
-id
-title
-body
-image
-slug
-timestamps
And in administrator, services page I created for Add, Edit, Delete it.
My problem is edit and update a image. A strange problem is that, when I change the of image to aaaaa field in the service table (database), Nothing happens. should happen. because I changed rename of image to aaaaa.
web.php
Route::resource('services', 'ServiceController');
ServiceController.php
public function edit(Service $service)
{
return view('Admin.services.edit', compact('service'));
}
public function update(Request $request, Service $service)
{
$service->title = $request->title;
$service->body = $request->body;
if($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/services'), $filename);
$service->image = $request->file('image')->getClientOriginalName();
}
$service->update();
return redirect()->route('services.index');
}
edit.blade.php
<form class="form-horizontal" action="{{ route('services.update', $service->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
#include('Admin.layouts.errors')
<div class="form-group">
<label for="title">عنوان</label>
<input type="text" class="form-control" id="title" name="title" placeholder="عنوان" value="{{ $service->title ? : old('title') }}">
</div>
<div class="form-group">
<label for="body">متن</label>
<textarea class="form-control" rows="10" id="body" name="body" placeholder="متن">{{ $service->body ? : old('body') }}</textarea>
</div>
<div class="form-group">
<label for="images">تصویر</label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="images" name="images">
<label class="custom-file-label" for="images">تصویر محصول</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">ذخیره</button>
</div>
</form>
Service.php
protected $fillable = [
'title',
'body',
'image',
'slug',
];
I even changed the controller in the update method as follows, nothing happened.
$service->save();
I think one of efficient way to edit image in laravel.
add this at top of the controller
use File;
First of get your bannerId in a variable
$bannerId = $request->banner_id;
get object of that id by find ().
$bannerData = FrontEndBanner::find($bannerId);
check if file exist and check if file already exist then delete that file else same name will inserted in imageName variable
if ($request->hasFile('banner_image')){
$image_path = public_path("/uploads/resource/".$bannerData->banner_name);
if (File::exists($image_path)) {
File::delete($image_path);
}
$bannerImage = $request->file('banner_image');
$imgName = $bannerImage->getClientOriginalName();
$destinationPath = public_path('/uploads/resource/');
$bannerImage->move($destinationPath, $imgName);
} else {
$imgName = $bannerData->banner_name;
}
$bannerData->title = $request->banner_title;
$bannerData->banner_name = $imgName;
$bannerData->save();
If anyone have issue then comment
In the html you have:
<input type="file" class="custom-file-input" id="images" name="images">
and in Controller: $request->file('image').
Replace
<input type="file" class="custom-file-input" id="images" name="images">
with
<input type="file" class="custom-file-input" id="images" name="image">
I'm working on a Laravel 5.4 app and the picture I try to upload is not moving to my disk.
Could you help me ?
My form:
<form class="form-horizontal" role="form" method="POST" action="{{ route('adminPostNews') }}" accept-charset="UTF-8" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('picture') ? ' has-error' : '' }}">
<label for="picture" class="col-md-1 control-label">Photo</label>
<div class="col-md-6">
<input type="file" id="picture" name="picture">
#if ($errors->has('picture'))
<span class="help-block">
<strong>{{ $errors->first('picture') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Ajouter
</button>
</div>
</div>
</form>
Controller:
public function store_actualites(Request $request)
{
$this->validate($request, [
'picture' => 'required|mimes:jpeg']);
if($request->file('picture'))
{
$file = $request->file('picture');
$extension = $file->getClientOriginalExtension();
Storage::disk('news')->put($file->getFilename().'.'.$extension, File::get($file));
}
}
My disk is ok, Laravel is giving the name of the file but the upload wont works.
You are not passing correctly the file name, try this:
//Save image
if ($request->hasFile('picture')) {
$file = $request->file('picture');
$fileName = $file->getClientOriginalName();
Storage::disk('news')->put($fileName, File::get($file));
}
ok, maybe the first answer will be: server file upload size limit !
Second, here is my new code:
$file = $request->file('picture');
$extension = $file->getClientOriginalExtension();
Storage::disk('news')->put($file->getFilename().'.'.$extension, file_get_contents($file));
I failed to use something different of "file_get_contents".