The "" file does not exist or is not readable - laravel

I am having some issue while trying to upload multiple images on the back end in Laravel. I have a simple form with an input field and a multiple attribute that should upload an array of images in the database but whatever I try, I get the same error 'The "" file does not exist or is not readable'. I checked the names in the input field and they are the same as the name in the file() method. Any help is appreciated.
PS: Am a newbie to Laravel and PHP...
ProductController:
foreach($request->file('images')->store('images') as $images) {
$product->images()->create([
'images' => $images
]);
}
Blade file:
<form method="POST" enctype="multipart/form-data">
#csrf
<h5>Upload Multiple Images</h5>
<input type="file" multiple name="images" id="images"> Upload Images
</form

I know this is an old question, but I had the same issue a few days ago. Hope this helps anyone.
I solved the problem by setting php_value upload_max_filesize in php.ini over the size of my uploaded file.
This is where I found the answer in case you are interested:
If someone else gets this error and it doesn't turn out to be a permissions issue, check your php.ini...make sure that your upload_max_filesize is as big as your post_max_size. I had 1000M for post_max_size (we deal with some big ol' video files), but only 100M for upload_max_size (I blame my old eyes). The upload would churn for a long time, and then throw the error above.
https://github.com/laravel/framework/issues/31249

Try..
$input = $request->all();
$datas = [];
if ($request->hasfile('images')) {
foreach ($request->file('images') as $key => $file) {
$name = $file->getClientOriginalName();
$file->move(public_path() . '/your path /', $name); //if you want to store image in yopur folder
$datas[$key] = $name;
$file = new YourMOdelNAme();
foreach ($datas as $data) {
$file->images = $data;
$file->save();
}
}
}

name images array
<form method="POST" enctype="multipart/form-data">
#csrf
<h5>Upload Multiple Images</h5>
<input type="file" multiple name="images[]" id="images"> Upload Images
</form

Upload files using below code
$files = $request->file('images');
if($request->hasFile('images'))
{
foreach ($files as $file)
$product->images()->create([
'images' => $file
]);
}

Related

Uploading files with infyom generator

I am trying to upload a file with laravel using the code generated by the infyom generator. The file seems to be uploaded but this is what is shown on the application when I view the report (C:\xampp\tmp\php7925.tmp). Provided below is the code for my application.
Thank you so much and really appreciate the help in this project.
rgds,
Form
<!-- Inf File Field -->
<div class="form-group col-sm-6">
{!! Form::label('inf_file', 'Attachments:') !!}
{!! Form::file('inf_file') !!}
</div>
Controller
{
$input = $request->all();
$infrastructure = $this->infrastructureRepository->create($input);
$file = $request->file('inf_file');
$file = $request->inf_file;
if ($request->hasFile('inf_file')){
//
if ($request->file('inf_file')->isValid()){
}
}
Flash::success('Infrastructure saved successfully.');
return redirect(route('infrastructures.index'));
}
This is how you display when you view your records,
<!-- Inf File Field -->
<div class="form-group">
{!! Form::label('inf_file', 'Attachements:') !!}
<a download href="{{ asset($infrastructure->inf_file) }}">Download</a>
</div>
Managed to solve it.
public function store(CreateinfrastructureRequest $request)
{
$input = $request->all();
if ($request->hasFile('inf_file')){
//Validate the uploaded file
$Validation = $request->validate([
'inf_file' => 'required|file|mimes:pdf|max:30000'
]);
// cache the file
$file = $Validation['inf_file'];
// generate a new filename. getClientOriginalExtension() for the file extension
$filename = 'Infras-' . time() . '.' . $file->getClientOriginalExtension();
// save to storage/app/infrastructure as the new $filename
$InfrasFileName = $file->storeAs('infrastructure', $filename);
$path = "/storage/app/public/".$InfrasFileName;
}
$input['inf_file'] = $path;
$infrastructure = $this->infrastructureRepository->create($input);
Flash::success('Infrastructure saved successfully. ' . $path);
return redirect(route('infrastructures.index'));
}

Laravel 5.8 image failed to upload

I cannot seem to upload an image file with Laravel. I keep getting the photo failed to upload.
My form:
<form id="save_report_form" action="{{ route('report.add') }}" method="post" enctype="multipart/form-data">
<input type="file" name="image" class="upload-photo" id="image" accept="image/png,image/jpg" />
</form>
My Controller:
public function add(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:png,jpg',
]);
// Get all file details and store in public
$disk = Storage::disk('public');
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = $file . '.' . $ext;
$disk->put($filename, file_get_contents($file), 'public');
return redirect()->back();
}
I have changed upload_max_filesize to 20mb for my dev server.
Where can I look to find the reason for the upload failure? I am not getting anything in the Laravel log. What have I missed. Thanks.
can you replace
$filename = $file . '.' . $ext;
with
$filename = $request->image->getClientOriginalName();
You can check what is the result of $filename with dd() /dd($filename) function and see if it is what you expect. If it is not, probably there is the problem.
Also as far as i see in the official docs(https://laravel.com/docs/5.8/filesystem#file-visibility) the usage of put() method is exampled without php native file_get_contents(check this also).

How to fix incomplete multiple image uploads

I have problem with uploading multiple images. The did successfully upload multiple images, but not every image was uploaded. Here is my form
{!! Form::open(['files'=>true,'url'=>'upload/file'])!!}
{!! Form::file('file[]',['multiple'=>'multiple']) !!}
{!! Form::submit('save') !!}
{!! Form::close() !!}
I tried to upload the specific type of images in my validation.
public function upload()
{
$this->validate(request(),['file.*'=>'required|image|mimes:jpg,jpeg,png']);
$files = request()->file('file');
foreach ($files as $file) {
$ext = $file->getClientOriginalExtension();
$file->move(public_path('uploads'),'image_'.time().'.'.$ext);
}
return back();
}
Sometimes it uploaded multiple images, but not all of the images were uploaded.
the problem is that:
i can't use time() because i upload these files at the same time
i used
Str::random(50)
instead

How to Set or retrieve old Path image when we not update the image in the form multiple images in Codeigniter 2.2.6

I have edit form in codeigniter in that form i have 15 more image file while i update the image it updating in database but even am not update the images in the form it goes and save as null or empty path .But i need solution for that when am not update all 15 files should retrive the same old image path which it is stored in database.Please Could you guys give better solution for this.
My Edit Form
controller:
if($this->input->post('submit'))
{
if($_FILES['file']['name']!='')
{
$filename = $_FILES['file']['name'];
$file_name1 = "upload_".$this->get_random_name()."_".$filename;
$_SESSION['file_upload']=$file_name1;
$file ="uploads/images/".$file_name1;
}
if($_FILES['file1']['name']!='')
{
$filename = $_FILES['file1']['name'];
$file_name2 = "upload_".$this->get_random_name()."_".$filename;
$_SESSION['file_upload']=$file_name2;
$file ="uploads/images/".$file_name2;
}
$query = $this->scener_model->popular_upload($file_name1,$file_name2)
}
If your is empty then pass hidden input value or else pass newly uploaded value like this..
if(!empty($_FILES()){
//your uploaded value here
} else {
//hidden attribute value
}
if($this->input->post('submit1')){
$titlemain = $this->input->post('title_main');
$price = $this->input->post('price');
$package = $this->input->post('package');
$titleinner = $this->input->post('title_inner');
$city_package = $this->input->post('city_packge');
if(!empty($count =count($_FILES['file1']['name']))){;
for($i=0; $i<$count;$i++){
$filename1=$_FILES['file1']['name'][$i];
//print_r($filename);exit;
$filetmp=$_FILES['file1']['tmp_name'][$i];
$filetype=$_FILES['file1']['type'][$i];
$images = $_FILES['file1']['name'];
$filepath="uploads/images/".$filename1;
move_uploaded_file($filetmp,$filepath);
}
$filename1 = implode(',',$images);
}
else{
$filename1=$this->input->get('iti_image2') ;
//print_r( $data['newz1']);exit;
}
my view page:
<div class="col-sm-8">
<input type="button" id="get_file" value="Grab file">
<?php
$imss= $result->iti_image2;
?>
<input type="file" id="my_file" name="file3[]" value="<?php echo $imss ?>" multiple/>
<div id="customfileupload">Select a file</div>
<input type="hidden" class="btn btn info" id="image" name="iti_image2" accept="image" value="<?php echo $result->iti_image2; ?>" multiple/ >
</div>
<script>
document.getElementById('get_file').onclick = function() {
document.getElementById('my_file').click();
};
$('input[type=file]').change(function (e) {
$('#customfileupload').html($(this).val());
});
</script>
</div>
my Model:
function itinerary_updte($filename4,$filename5){
$update_itinerarys = array(
'iti_image2' =>$filename4,
'iti_image3' =>$filename5
);
$this->db->where('id', $id);
$result = $this->db->update('sg_itinerary', $update_itinerarys);
return $result;
}
Finally guys i found a solution for image updating follwing some tutorials see the link
"http://www.2my4edge.com/2016/04/multiple-image-upload-with-view-edit.html"
thanks buddies who are give the logic and commenting on my post and thanks alot #ankit singh #Lll

How to store multiple images paths into database in laravel?

I am working on multiple image upload in laravel. Uploading is successful but, i got an issue. For later use i should store in database. For single column it easy but for different column for different images i couldnot do it.
Here is my image upload code
$images = [];
$destDir = "public/uploads/";
if($request->file('image1')) $images[]=$request->file('image1');
if($request->file('image2')) $images[]=$request->file('image2');
if($request->file('image3')) $images[]=$request->file('image3');
foreach($images as $image)
{
if(!empty($image))
{
$imageName = $image->getClientOriginalName();
$extension = $image->getClientOriginalExtension();
$temPath = $image->getRealPath();
$enImg = mt_rand(1111,9999).".".$extension;
$newPath = $destDir.$enImg.".".$extension;
move_uploaded_file($temPath, $newPath);
}
}
I have a feeling you are approaching this in a way that is making it more difficult for you. Instead of creating multiple separate file inputs in your form you can only set a multiple file input and set the name of the input to an array like so:
<form action="demo_form.asp">
Images: <input type="file" name="images[]" multiple>
<input type="submit">
</form>
Then in your php you can do something like this:
if (isset($request->all()['images'])) {
$this->persistUserImages($request->all()['images'], $userId);
}
/**
* Persists user images to storage and DB
*
* #param array $userImages
* #param int $userId
* #return array
*/
private function persistUserImages(array $userImages, $userId = 1)
{
if (empty($userImages)) {
return [];
}
$uploadedUserImagesFilenames = $this->uploadUserImagesToCloud($userImages, $userId);
foreach ($uploadedUserImagesFilenames as $filename) {
$userImage = new UserImage([
'user_id' => $userId,
'filename' => $filename,
'visible' => 1
]);
$userImage->save();
}
}
You can do like this this. In your blade,
<form action="{{url('path')}}" methos="post" enctype="multipart/form-data">
Images: <input type="file" name="images[]" multiple>
<input type="submit">
</form>
In you method,
public function store(Request $request){
foreach ( $request->images as $image) {
Model::create([
'image_url' => $image,
]);
}
}
Or
follow below link. It's very easy way to solve this problem.
Multiple Image Upload In Laravel
Hope you can solved your problem.
create a pivot table and store images in separate rows..
Table schema:- `images` table
columns `id`,`user_id`(foreign key),`image_path`
store your $newPath in this table every time..

Resources