when I am trying to upload an image there is no error being shown or anything coming out. All it does was return me back to the create1.blade.php. When I try to use dd, it also doesn't show anything as well(maybe I put it wrongly or something not sure, still learning about it)
Here are my codes:
Controller:
public function create1(){
return view('create1');
}
public function store1(Request $request){
$this->validate($request, [
'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('input_img')) {
$image = $request->file('input_img');
$name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$this->save();
return redirect()->back()->with('success','Image Upload successfully');
}
}
create1.blade.php
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="imageInput" class="control-label col-sm-3">Upload Image</label>
<div class="col-sm-9">
<input data-preview="#preview" name="input_img" type="file" id="imageInput">
<img class="col-sm-6" id="preview" src="" ></img>
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
You just forgot to put enctype="multipart/form-data" in your <form> tag!
This is allow to upload the file!
For example:
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
Hope this fixed your issue!
The issue is here:
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}">
here you are missing enctype="multipart/form-data". Put it in form like:
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
and try again.
Related
I was trying to convert the image uploaded into a form into base 64 in Laravel but the uploaded image is null
Blade File-->(View)
<body>
<form action="{{Route('PostImageProcess')}}" enctype='multipart/form-data'>
{{ csrf_field() }}
<div class="form-group">
{{-- {!! Form::label($for, $text, [$options]) !!} --}}
<label for="img">post Image</label>
<input type="file" name="image" id="img">
</div>
<input type="submit">
</form>
</body>
Inside Controller-->
public function PostImageProcess(Request $request){
// $image = base64_encode(file_get_contents($request->file('image')));
$ima= base64_encode(file_get_contents($request->file('image')));
echo $ima;
$image="11221";
return view('afterPostSuccess')->with("body",$image);
}
and I am getting this error: file_get_contents(): Filename cannot be empty
You are not validating your Request $request
$valid = $request->validate([
'image' => 'required|max:100024',
]);
$ima= base64_encode(file_get_contents($request->file('image')));
echo $ima;
$image="11221";
return view('afterPostSuccess')->with("body",$image);
UPDATE
Edit your form like:
<body>
<form method="post" action="{{Route('PostImageProcess')}}" enctype='multipart/form-data'>
{{ csrf_field() }}
<div class="form-group">
{{-- {!! Form::label($for, $text, [$options]) !!} --}}
<label for="img">post Image</label>
<input type="file" name="image" id="img">
</div>
<input type="submit">
</form>
</body>
I'm trying to save an image from a form into database. I tried this but it's not working:
public function store(Request $request)
{
$article = new article;
$photo_articles = new photo_articles;
// $type = new type;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->id = auth()->user()->id;
$article->TYPE_ARTICLE = $request->LABEL_TYPE;
$article->save();
$photo_articles->PHOTO_ARTICLE = base64_encode(file_get_contents($request->PHOTO_ARTICLE));
$photo_articles->ID_ARTICLE = $article->ID_ARTICLE;
$photo_articles->save();
return;
}
Here is my form:
<form method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-6">
<div class="col-lg-12">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article"
required="required">
</div>
<div class="col-lg-12">
<select class="contact_input" name="LABEL_TYPE">
#foreach($types as $type)
<option> {{$type->LABEL_TYPE}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-6">
<div id="uploading" class="uploadfile">
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input uploadFileInput" id="imagearticle" name="PHOTO_ARTICLE"
placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<p id="uploadtextid" class="uploadText">upload image</p>
<img class="uploadImage" src="" id="displayedimage">
</div>
</div>
<div class="col-lg-12">
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
</div>
<button class="contact_button right" type="submit">Valider!</button>
</div>
</form>
My image is in $request->PHOTO_ARTICLE.
Can someone show me how to save it as base64? I've searched a lot but without result.
When you're submitting a file with a form you have to set add the attribute
enctype="multipart/form-data" to the opening form tag:
<form enctype="multipart/form-data" method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
otherwise it will only submit the name of the file and not the file itself.
Then in your route you would just need:
$photo_articles->PHOTO_ARTICLE = base64_encode(
file_get_contents($request->file('PHOTO_ARTICLE')->path())
);
I'm trying to upload an image via an HTML form in Laravel 5.5. I have included the enctype="multipart/form-data" attribute, still nothing happens.
Form code:
<form method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="m_photo" class="col-md-4 control-label">Main photo</label>
<div class="col-md-6">
<input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Route (web.php) code:
Route::post('smartphones/entry', 'HomeController#s_submit')->name('s_submit');
Controller code:
public function s_submit() {
if (Input::hasFile('m_photo')) {
// doing something
}
else {
echo 'Nothing happened';
}
}
'Nothing happened' is echoed out when I submit the form.
It's interesting that when I do this:
public function s_submit(Request $request) {
$input = Input::all();
dd($input);
}
I see:
array:1 [
"m_photo" => UploadedFile {#210 ▶}
]
It's like the image is getting passed, but I'm unable to retrieve it. Please help.
This can happen when PHP max_file_size is not set to a size that allows the file you are trying to upload to be sent. This causes hasFile returns false, when, for example, file->getClientOriginalName() works.
Try to check upload_max_filesize or post_max_size in your php.ini, or try with a smaller file to check if it works.
if (Input::hasFile('m_photo')) {
$destinationPath = '/uploads/app/';
$file = $request->file('m_photo');
$filename = $file->getClientOriginalName();
$file->move(public_path() . $destinationPath, $filename);
$filename_to_save_in_db = $destinationPath . $filename;
}
Get the file with:
$file = $request->m_photo;
Or with:
$file = $request->file('m_photo');
https://laravel.com/docs/5.5/requests#retrieving-uploaded-files
You have forgot to put action in Your html Form :
put action="/smartphones/entry" or action="{{route('s_submit')}}"
<form method="POST" enctype="multipart/form-data" action="{{route('s_submit')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="m_photo" class="col-md-4 control-label">Main photo</label>
<div class="col-md-6">
<input id="m_photo" type="file" class="form-control-file space" name="m_photo" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
Create images folder in public folder
in your controller
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
public function s_submit(Request $request) {
if($request->hasFile('m_photo')){
$filenameWithExt=$request->file('m_photo')->getClientOriginalName();
$filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
$extension=$request->file('m_photo')->getClientOriginalExtension();
$fileNameToStore=$filename.'_'.time().'.'.$extension;
request()->m_photo->move(public_path('images'), $fileNameToStore);
}
else{
$fileNameToStore='noimage.jpg';
}
}
Try this:
public function s_submit()
{
request()->validate([
'm_photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->m_photo->getClientOriginalExtension();
request()->m_photo->move(public_path('images'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('m_photo',$imageName);
}
Blade:
{!! Form::open(array('route' => 's_submit','files'=>true)) !!}
<div class="form-group">
<label for="m_photo" class="col-md-4 control-label">Main photo</label>
<div class="col-md-6">
{!! Form::file('m_photo', array('class' => 'form-control-file space')) !!}
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
For future visitors, the accepted answer is the right answer.
I wanted to share one more thing.
I also faced similar issue and I had set the upload_max_filesize and post_max_size variables to 20M, which was quite enough.
But I still faced the issue. So, I increased to 500M, then it worked.
It was really strange because I was uploading a file of less than 1 MB size.
I don't know where do I doing wrong the file not uploaded and the name not store in database.
Here's my controller
if(Input::hasFile('photo')) {
$fotoName = 'peg' . $employee->id . '.' .
$request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move(
base_path() . '/public/images/employee/', $fotoName
);
$img = Image::make(base_path() . '/public/images/employee/' . $fotoName);
$img->resize(150, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save();
$employee_fotos = Karyawan::find($employee->id);
$employee_fotos->photo = $fotoName;
$employee_fotos->save();
}
Views
<form class="form-horizontal" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
{{ csrf_field() }}
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
<h6>Upload a different photo...</h6>
<input type="file" name="photo" class="form-control" />
</div>
</div>
</form>
and I dont get any error and if I make validate its always get please add image or make sure the file extention .jpg etc, for sure I have choice a right image and extention
add enctype="multipart/form-data" to your form tag:
<form class="form-horizontal" enctype="multipart/form-data" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
{{ csrf_field() }}
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
<h6>Upload a different photo...</h6>
<input type="file" name="photo" class="form-control" />
</div>
</div>
</form>
public function store(Request $request)
{
//dd(request()->all());
$this->validate(request(), [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6000',
]);
$imageName = time().'.'.$request->file->getClientOriginalExtension();
$request->file->move(public_path('/images/employee/'), $imageName);
}
This should work for you but it will store image with a unix timestamp name. If you use the employer id to store images you might have duplicate images in your storage folder next time you upload a new image with the same employee account.
Also add enctype="multipart/form-data" to your form attributes.
Image Upload not working all round, the problem is the browser is just not giving the full file path for the server to upload.
To verify you can do an alert on the value of the input type to see what I mean
<input type="file" name="photo" onchange="alert(this.value)" class="form-control" />
Go over to How to resolve the C:\fakepath? for the full solution to the problem
read the answer from 'chakroun yesser'
Im having trouble with my form with Laravel 5. When I specify the enctype attribute to 'multipart/form-data' I receive a token mismatch error. If it is removed, the form always fails the validation specified in my controller.
HTML
<form class="lajax" action="{{ action('AlbumController#store') }}" method="POST">
<div class="form-group">
<label>Album Name</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label for="coverFile">Album Cover Image</label>
<input name="cover" type="file" id="coverFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="form-group">
<label for="albumFiles">Album Images</label>
<input type="file" name="photos[]" multiple>
</div>
<button type="submit" class="btn btn-primary">Create Album</button>
{{ csrf_field() }}
</form>
Controller
public function store(Request $request)
{
//request input verification rules
$rules=[
'name'=>'required',
'cover'=>'required|image',
'photos'=>'required|array',
'photos.*'=>'image'
];
//perform validation
$this->validate($request,$rules);
// blah blah
}
Specifically, the images seem to be failing.
Errors Reported: cover is not an image, photo.0 is not an image, photo.1 is not and image..... and so forth.
Please help
Change:
<form class="lajax" action="{{ action('AlbumController#store') }}" method="POST">
To:
<form method="POST" action="{{ action('AlbumController#store') }}" accept-charset="UTF-8" enctype="multipart/form-data">
In your controller you can check your input like this:
$request->hasFile('file_input_name');
Also check Laravel Collective to create forms: https://laravelcollective.com/
I have found the error! It was in my php.ini file. I change post_max_size fom 3M to 1000M. it worked.