Laravel The picture failed to upload - 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".

Related

When uploading multiple images then without saving images 403 error appered

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.

laravel livewire Save file using file name

I'm trying to save uploaded pdf files but the pdf file name changes on the storage link. is there any way to retain the original file name when saving?
public $code, $pdfs;
public function mount(Applicant $applicant)
{
$this->code = substr(str_shuffle(str_repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 5)), 0, 3).Carbon::createFromFormat('Y-m-d H:i:s', now())->format('md').rand(100, 999);
}
public function submit(Request $request)
{
$this->validate([
'pdfs.*' => 'mimes:pdf',
]);
$filenames = collect($this->tests)->map->store($this->code.'/', 'public');
return redirect()->route('careers.vacant');
}
here's my blade
<form wire:submit.prevent="submit" class="pt-3" enctype="multipart/form-data">
<div class="form-group">
<label class="form-label required" for="code" >Application {{ trans('fields.code') }}</label>
<input class="form-control" type="text" name="code" id="code" wire:model.defer="code" >
<div class="validation-message">
{{ $errors->first('code') }}
</div>
<div class="help-block">
{{ trans('fields.code_helper') }}
</div>
</div>
<input type="file" name="pdf" id="pdf" wire:model="pdfs" multiple >
<div wire:loading wire:target="pdfs">Uploading...</div>
#error('pdfs.*') <span class="error">{{ $message }}</span> #enderror
<div class="form-group">
<button class="mr-2 btn btn-indigo" type="submit">
{{ trans('global.submit') }}
</button>
<a href="{{ route('admin.applicants.index') }}" class="btn btn-secondary">
{{ trans('global.cancel') }}
</a>
</div>
</form>
I need to save the pdfs like this:
$filenames = collect($this->tests)->map->store($this->code.'/'.pdfFileName, 'public');
EDIT:
foreach ($this->tests as $file) {
$name = $file->getClientOriginalName();
$file->store('moca/'.$this->code.'/'.$name, 'public');
}
I tried this code but in the path $name becomes a folder instead of becoming the name of the file
You can try this:
$request->file('pdf')->getClientOriginalName() will return the original name of file

I am trying to update multiple images but getting error using laravel 8

I am trying to update multiple images but unfortunately I am getting error please help me how can i resolved that ? thank u.
please check error
Invalid argument supplied for foreach()
controller
public function update(Request $request, $roomId)
{
if($request->has('images')){
foreach($request->file('images') as $value){
$extension = $value->getClientOriginalExtension();
Storage::disk('wfh')->put($value->getFilename() . '.' . $extension,
File::get($value));
$roomDetail->image = $value->getFilename() . '.' . $extension;
$roomDetail = RoomDetail::where('room_id',$roomId)->first();
$roomDetail->update([
'room_id' => $roomId,
'image' => $roomDetail->image,
]);
}
}
Html view
<form action="{{route('room.update',$editRooms->id)}}" method="POST" class="needs-validation"
novalidate enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row">
<div class="col-md-8">
<div class="input-field">
<label class="active">Images Upload</label>
<div class="input-images" style="padding-top: .5rem;" ></div>
#foreach ($roomDetails as $value)
<img height="100px" class="mt-4 m-4" width="100px" src="{{
Config('wfh.file')
.$value->image}}" alt="">
<input type="hidden" name="images[]" value="{{$value->image}}" >
#endforeach
</div>
</div>
</div>
<div class="col-md-3">
<div class="modal-footer">
<button type="submit" id="btnSubmit" class="btn btn-primary btn-lg w-100">Add
Room</button>
</div>
</div>
</form>

Can't store Files in public folder in Laravel 8

i'm trying to store pdf, txt... in DB
but i get this error:
Method Illuminate\Validation\Validator::validateCsv,txt,xlx,xls,pdf does not exist.
but when i add required|mimes this error i get:
The file must be a file of type: pdf, xlx, csv, txt, doc.
here the code:
public function store(Request $request)
{
$validatedData = $request->validate([
'file' => 'required|mimes:pdf,xlx,csv,txt,doc'
]);
$name = $request->file('file')->getClientOriginalName();
$path = $request->file('file')->store('public/files');
$save = new File;
$save->name = $name;
$save->path = $path;
return redirect('file-upload')->with('status', 'File Has been uploaded successfully');
}
my blade code:
<body>
<div class="container mt-4">
<form method="post" enctype="multipart/form-data" id="upload-file" action="{{ url('store') }}" >
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="file" name="file" placeholder="Choose file" id="file">
#error('file')
<div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
#enderror
</div>
</div>
<div class="col-md-12">
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</div>
</div>
</form>
</div>
</div>
</body>
how can i fix it?
Use File to put content
\File::put(public_path('storage'). '/public/files/'. $file);

Update image in laravel won't work

I'm trying to update image in my laravel project but it won't save the image, I'm using intervention package
this is the result i use dd
Controller:
public function update(Request $request, $id)
{
//
$this->validate($request, [
'student_name'=>'required|max:50',
'lead_status'=>'required|max:50',
'lead_source'=>'required|max:50',
'avatar' =>'image',
]);
$leads = Lead::findOrFail($id);
$leads->student_name = $request->student_name;
$leads->student_nric = $request->student_nric;
$leads->gender = $request->gender;
$leads->religion = $request->religion;
$leads->race = $request->race;
$leads->date_of_birth = $request->date_of_birth;
$leads->Address = $request->Address;
$leads->last_school_attended= $request->last_school_attended;
$leads->last_grade_completed = $request->last_grade_completed;
$leads->grade_appliying = $request->grade_appliying;
if($request->hasFile('avatar')){
$image=$request->file('avatar');
$filename=time() . '.' . $image->getClientOriginalExtension();
$location=public_path('images/' .$filename);
Image::make($image)->resize(300, 300)->save($location);
$leads->image=$filename;
$leads->save();
}
else{
$leads->save();
session()->flash('notif','Application Saved Successfully');
return view('leads.edit')->with('leads', $leads);
}
}
Edit.Blade View
<div class="row">
<div class="col-sm-4 form-group">
<img src="/uploads/avatars/{{$leads->image}}" style="width:150px;height:150px;border-radius:50px;">
<form enctype="multipart/form-data" action="{{route('leads.update', $leads->id)}}" method="POST" >
{{ csrf_field() }}
{{ method_field('PUT') }}
<label> Upload Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
<div class="col-sm-12">
<h4 text-muted>CHILD'S INFORMATION</h3>
<hr>
<div class="row">
<div class="col-sm-4 form-group">
<label>FULLNAME</label>
<input class="form-control" style="text-transform: uppercase;" type="text" name="student_name" value="{{$leads->student_name}}" placeholder="Enter FULLNAME.." autocomplete="off">
</div>
Note: i will always end up null value on my image please point me to a correct tutorial if u have..
......................................................................................................................................
Add a hidden input type with your database variable in your edit blade
and put the value of your hidden input name in else condition to save.
<input type="hidden" name="old_image_name"
value="#if(isset($admin_details)){{$admin_details->image}} #endif"/>

Resources