i have this code in my controller that can make me upload successfully just one file , and i want to upload many files in once time :
public function store(Request $request, $id) {
$request->validate([
'image' => 'required',
]);
$listing = Listing::findOrFail($id);
$image = new Listingimage();
if ($request->hasFile('image')) {
$file = $request->file('image');
$extention = $file->getClientOriginalExtension();
$filename = time() . '.' . $extention;
$file->move('assets/images/listingimages/', $filename);
$fileOriginalName = $file->getClientOriginalName();
}
$image->listing_id = $id;
$image->image_url = $filename;
$image->nom_image = $fileOriginalName;
$image->save();
return redirect()->back();
}
i use also this input :
<form action="{{ route('Listingimages.store', $listing->id) }}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT" />
{{csrf_field()}}
{{method_field('PUT')}}
<label> Insert image</label>
<input type="file" name="image" id="files" class="form-control" multiple>
so , how can i upload many files in once time ?
change input name
<input type="file" name="image[]" id="files" class="form-control">
controller
public function store(Request $request, $id) {
$request->validate([
'image' => 'required',
]);
$listing = Listing::findOrFail($id);
if ($request->hasFile('image')) {
foreach($request->file('image') as $file)
{
$image = new Listingimage();
$file = $request->file('image');
$extention = $file->getClientOriginalExtension();
$filename = time() . '.' . $extention;
$file->move('assets/images/listingimages/', $filename);
$fileOriginalName = $file->getClientOriginalName();
$image->listing_id = $id;
$image->image_url = $filename;
$image->nom_image = $fileOriginalName;
$image->save();
}
}
return redirect()->back();
}
Related
I'm making some kind of form be able to upload PDF file. And I also make a label to preview the file that I'm uploading. But somehow the label wont preview the file with error massage : refused to connect. Can somebody help me with it?
image form preview
So here's the PHP code for the label on the view file that I use :
<div class="form-group row">
<label class="col-md-2 col-form-label">File</label>
<div class="col-md-6">
<input type="file" name="file" id="file" class="form-control" required>
<iframe src="{{ $data['data']['files'] }}" style="width: 1000px; height:500px;" frameborder="0" id="filePreview" class="mt-2"></iframe>
</div>
<small class="text-small">{{ $errors->first('file') }}</small>
</div>
And here is the edit and update controller that I use for that view
public function edit($id)
{
$system = new SystemService();
$systemData = $system->list_badges();
$data['system'] = $systemData['data'];
$api = new SettingService();
$data['data'] = $api->get_about($id);
$data['title'] = 'Ubah Tentang Aplikasi';
$data['default'] = $this->folder;
$data['data_view'] = $this->folder . '/edit';
return view('template', $data);
}
public function update(Request $request, $id)
{
$api = new SettingService();
if ($request->hasFile('file')) {
$file = $request->file('file');
$mime = $file->getClientOriginalExtension();
$filter_file = [
'pdf',
'doc', 'docx',
'csv', 'xls', 'xlsx',
'png', 'jpg', 'jpeg',
];
if (!in_array($mime, $filter_file)) {
return back()->with('failed', 'file extention not correct');
}
$file_path = $request->file('file')->path();
$file_name = md5(date('YmdHis') . $file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
$data['file_path'] = $file_path;
$data['file_name'] = $file_name;
}
$result = $api->update_about($id, $data);
if (isset($result['errors'])) {
return redirect($this->folder . '/' . $id . '/edit')->withErrors($result['errors'])->withInput()->with('failed', $result['message']);
}
if ($result['status'] == false) {
return redirect($this->folder . '/' . $id . '/edit')->withInput()->with('failed', $result['message']);
}
return redirect($this->folder)->with('success', $result['message']);
}
Thankyou
I have searched for the SO and didn't find any article or post related to this.
How do I upload an Image using the Image Intervention and upload a normal file with in a single forum without opening a new page for the uploads.
Hope the below Answer would help someone out there.
Blade
<form action="{{route('index.store')}}" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label for="resume_path">Resume</label>
<input type="file" class="form-control"
name="resume_path">
</div>
<div class="form-group">
<label for="engineer_avatar">Profile Image</label>
<input type="file" class="form-control"
name="engineer_avatar">
</div>
</form>
Controller
use Image;
use App\Engineers;
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'engineer_avatar' => 'image|mimes:jpeg,png,jpg|max:2048',
'resume_path' => 'file|mimes:doc,docx,pdf|max:2048',
// dimensions:min_width=600,min_height=400'
]);
$engineers = Engineers::findOrFail($id);
if($request->hasFile('engineer_avatar')){
$image = $request->file('engineer_avatar');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/engineer_avatar/' . $filename);
Image::make($image)->resize(600,400)->save($location);
$engineers->avatar_path = $filename;
}
if($request->hasFile('resume_path')){
$file = $request->file('resume_path');
$file_name1 = time() . '.' . $file->getClientOriginalExtension();
$file_path = public_path('resume/engineer/');
$engineers->resume_path = $file_name1;
$file->move($file_path, $file_name1);
$engineers->save();
}
To delete the file ::
public function destroy($id)
{
$engineers = Engineers::findOrFail($id);
unlink(public_path('images/engineer_avatar/' . $engineers->avatar_path ));
unlink(public_path('resume/engineer/' . $engineers->resume_path ));
$engineers->delete();
}
I want to update an image in laravel 5.6.
In my app, the image is uploading and inserting into the database correctly but when I want to update the image it cannot update the old image.
How can I update the image with other field?
This is my view:
<form method="post" action="{{ route('employee.update', $employee->id) }}" enctype="multipart/form-data">
#csrf
#method("PATCH")
<input type="hidden" name="profile_old_image" value="{{$employee->profile_pic}}">
<div>
<input type="file" name="profile_pic" class="form-contrle" >
</div>
<div>
<input placeholder='Your Name' type='text' name='name' value="{{$employee->name}}" class="form-control">
</div>
</form>
This is my controller:
public function update(Request $request, $id)
{
$rules = [
'name' => 'required',
'father_name' => 'required',
'phone_no' => 'required',
'cnic' => 'required'
];
$validate = $this->validate($request, $rules);
$file = $request->file('profile_pic');
if($file == '')
{
$profile_pic = $request->profile_old_image;
}
else
{
$file = $request->file('profile_pic');
$fileName = time().'.'.$file->getClientOriginalExtension();
$destinationPath = public_path('/admin/images/employees');
$file->move($destinationPath,$fileName);
$profile_pic = '/admin/images/employees/'.$fileName;
}
//dd($request->all());
$employee = Employee::find($id);
$employee->name = $request->name;
$employee->father_name = $request->father_name;
$employee->phone_no = $request->phone_no;
$employee->cnic = $request->cnic;
$employee->desg_id = $request->desg_id;
$employee->profile_pic = $profile_pic;
$employee->save();
return redirect('admin/employee')->with('message', 'Records Successfully Updated');
}
Currently, I have 2 upload forms and 2 functions, uploadImage(); and uploadAlbum();. I have been wondering if I could remove the single image form and use the multi image form for both cases. If only 1 image is selected in the multi image form, a single image would be uploaded and if more than 1 images are uploaded, an album would be uploaded.
That would make the upload view look better since it won't have 2 identical upload forms and it would only require 1 function on the back-end that would determine whether it's a single image or an album based on the amount of images uploaded.
I don't really see any downsides to it but I wanted to make sure before reworking the code.
My upload view:
<form class='uploadForm' action="{{ route('imageUpload') }}" method="POST" enctype="multipart/form-data">
<label for="name">Image Name</label>
<input class='input' type="text" name="name" placeholder="Image Name">
<label for="description">Image Description</label>
<input class='input' type="text" name="description" placeholder="Description">
<input type="file" name="image"> {{ csrf_field() }}
<button class='Submit' type="submit" name="submit">UPLOAD</button>
</form>
<form class='uploadForm' action="{{ route('albumUpload') }}" method="POST" enctype="multipart/form-data">
<label for="albumName">Album Name</label>
<input class='input' type="text" name="albumName" placeholder="Album Name">
<label for="albumDescription">Image Description</label>
<input class='input' type="text" name="albumDescription" placeholder="Description">
<input type="file" name='files[]' multiple> {{ csrf_field() }}
<button class='Submit' type="submit" name="submit">UPLOAD</button>
</form>
My uploadImage() and uploadeAlbum() functions:
public function uploadAlbum(Request $request){
$name = $request['albumName'];
$description = $request['albumDescription'];
$tag = $request['tags'];
$userId = auth()->user()->id;
$files = $request->file('files');
$path = 'storage/uploads/albums/'.$name;
$fileOriginalName = $files[0]->getClientOriginalName();
$fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
$extension = $files[0]->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$album = new Album();
$album->name = $name;
$album->description = $description;
$album->user_id = $userId;
$album->thumbnail = $fileNameToStore;
$album->save();
$album->tags()->attach($tag);
if(!File::exists($path)) {
File::makeDirectory(public_path($path));
}
if (is_array($files) || is_object($files)){
foreach ($files as $file){
$fileOriginalName = $file->getClientOriginalName();
$fileName = pathinfo($fileOriginalName, PATHINFO_FILENAME);
$extension = $file->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$file->storeAs('public/uploads/albums/'.$name, $fileNameToStore);
$file->storeAs('public/uploads/albums/'.$name.'/thumbnails/', $fileNameToStore);
$thumbnailImage = InterventionImage::make('storage/uploads/albums/'.$name.'/thumbnails/'.$fileNameToStore)->fit(400, 400, function ($constraint) {
$constraint->upsize();
});
$thumbnailImage->save();
$albumImage = new AlbumImage();
$albumImage->file_name = $fileNameToStore;
$albumImage->album_id = $album->id;
$albumImage->save();
}
}
return redirect()->route('albums');
}
public function uploadImage(Request $request){
$this->validate($request, [
'name' => 'required|max:120',
'description' => 'max:120|nullable',
'image' => 'required'
]);
$name = $request['name'];
$description = $request['description'];
$tag = $request['tags'];
$userId = auth()->user()->id;
$file = $request->file('image')->getClientOriginalName();
$fileName = pathinfo($file, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$fileNameToStore = str_replace(' ', '', $fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/thumbnails/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/specificImages/',$fileNameToStore);
$request->file('image')->storeAs('public/uploads/images/miniImages/',$fileNameToStore);
$thumbnail = InterventionImage::make('storage/uploads/images/thumbnails/'.$fileNameToStore )->resize(500, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$thumbnail->save();
$specificImage = InterventionImage::make('storage/uploads/images/specificImages/'.$fileNameToStore )->resize(2000, null, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
$specificImage->save();
$miniImage = InterventionImage::make('storage/uploads/images/miniImages/'.$fileNameToStore )->fit(200, 200, function ($constraint) {
$constraint->upsize();
});
$miniImage->save();
$image = new Image();
$image->name = $name;
$image->description = $description;
$image->user_id = $userId;
$image->file_name = $fileNameToStore;
$image->save();
$image->tags()->attach($tag);
return redirect()->route('home');
}
This is possible of course. You would have to use the field that allows multiple
<input type="file" name="files[]" multiple />
When submitting the form you can check for if the $_POST['files'] array contains only one file. If it does, you can use the logic of a single file (image) and if it contains more you can use the logic of multiple files (album).
When you have this working you can also merge the majority of your logic and split it into multiple functions. One would be called with a foreach.
I am trying to upload multiple images in a single row in a database table and access them in a show page.I have tried this tutorial:
laraveldaily.com/upload-multiple-files-laravel-5-4/
but there two different tables are made and a relation is established.
I want this to happen in a single table.
here is what worked best for me:
first do this in your form:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
Now do this in your controller:
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}
Hope this works
<form role="form" method="post" action="{{URL::to('addimage')}}" enctype="multipart/form-data">
<div class="form-group" style="padding-bottom: 15px">
<label class="col-lg-3">Upload</label>
<input class="btn btn-primary" type="file" name="files[]" > <br/>
</div>
</form>
$images = $request->file('files');
if ($request->hasFile('files')) :
foreach ($images as $item):
$var = date_create();
$time = date_format($var, 'YmdHis');
$imageName = $time . '-' . $item->getClientOriginalName();
$item->move(base_path() . '/uploads/file/', $imageName);
$arr[] = $imageName;
endforeach;
$image = implode(",", $arr);
else:
$image = '';
endif;
DB::table('fooo')->insert(
array(
'image' => $image
)
);
Session::flash('message', 'Image upload successfully successfully');
return redirect('/addimage');
this can also be achieved with
foreach ($request->file('files') as $index => $item) {
$path = $request->files[$index]->store('images');
}
html
<input type="file" name="images[]" multiple>
controller : store folder storage
foreach ($request->file('images') as $imagefile) {
return $imagefile->store('images','public');
}
This one works for me.
Form:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection image:
<input required type="file" class="form-control" name="image[]" placeholder="address" multiple>
Now do this in Our Model:
protected fillable = ['producd','image', ];
Now do this in Our controller:
public function store(Request $request)
{
$image = array();
if($file = $request->file('image')){
foreach($file as $file){
$image_name = md5(rand(1000,10000));
$ext = strtolower($file->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$uploade_path = 'uploads/images/';
$image_url = $uploade_path.$image_full_name;
$file->move($uploade_path,$image_full_name);
$image[] = $image_url;
}
Image::insert([
'image' => implode('|', $image),
'producd' => $request->producd,
]);
return redirect('/image.index'));
}
}
$images = array();
if ($request->hasFile('gallery_image')) {
$files = $request->file('gallery_image');
foreach ($files as $file) {
$image_name = md5(rand(1000, 10000));
$ext = strtolower($file->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$file->move('Your image path here',
$image_full_name);
$images[] = $image_full_name;
}
}
$house->gallery_image = json_encode($images);