Invalid argument supplied for foreach() while uploading images Image upload works in my old Project the same code does not works in new project give me error in the controller
$files = Input::file('filename');
$uploadcount = 0;
foreach($files as $file) {
$destinationPath = 'images';
$filename = $file->getClientOriginalName();
$file->move($destinationPath,$filename);
$uploadcount ++;
$file->getClientOriginalExtension();
$entry = new \App\Product();
$entry->mime = $file->getClientMimeType();
$entry->filename = $filename;
$entry->save();
}
in the blade file
<h2>Image Upload</h2>
{!! Form::open(array('url'=>'/image/update','method'=>'POST', 'files'=>true)) !!}
{!! Form::file('filename[]',array('multiple'=>true, 'id' => 'file',
<br><br>
{!! Form::submit('Submit', array('class'=>'btn btn-lg btn-primary col-md-4')) !!}
{!! Form::close() !!}
No idea, but put dd($files) before foreach, it seems empty...
it seems empty when i try with dd($files) uploading image, but image name shows beside the browse button
This is my Sample Code
$inputs = $request->rows;
$collection = array();
$var_thumb_id = array();
foreach ($inputs as $input) {
if (!empty($input['img'])) {
$image = $input['img'];
$image_thumb_id = 0;
$user_are_inserting = Auth::id();
$uploaded = Image::make($image)->fit(1000, 1000);
$arkapilan = Image::make(asset('public/gokcekmarket.jpg'))->fit(1000, 1000);
$image = Image::canvas(1000, 1000);
$image->insert($arkapilan);
$image->insert($uploaded);
$ext = pathinfo($_FILES['thumb_img']['name'], PATHINFO_EXTENSION);
$filename = time() . '.' . $ext;
$path = public_path('images/' . $filename);
$path74 = public_path('images/74/' . $filename);
$path170 = public_path('images/170/' . $filename);
$path403 = public_path('images/403/' . $filename);
if ($image->save($path)) {
Image::make($path)->fit(403, 403)->save($path403);
Image::make($path)->fit(170, 170)->save($path170);
Image::make($path)->fit(74, 74)->save($path74);
$insert = new Images;
$insert->id = null;
$insert->user_id = $user_are_inserting;
$insert->parent_id = 0;
$insert->describe = $request->title;
$insert->img_path = $filename;
$insert->save();
$image_thumb_id = $insert->id;
} //if insert image
Call to a member function getClientOriginalName() on string gets error after debugging to get image file name by using dd($files)
use this function instead of it!
$ext = pathinfo($_FILES['thumb_img']['name'], PATHINFO_EXTENSION);
@satiseven777 Sir it shows me Invalid argument supplied for foreach() as error again!!
@satiseven777 the same code works perfectly in my another project
Why dont you try this in your view
<form method="POST" action="/image/update" enctype="multipart/form-data" class="box-typical">
{{ csrf_field() }}
<input type="file" id="file-input" class="gallery-photo-add" name="files[]" multiple accept="image/*">
<button type="submit"> Submit </button>
</form
and in your controller
if(! is_null(request('files')))
$files=request('files');
foreach($files as $file) {
$destinationPath = 'images';
$filename = $file->getClientOriginalName();
$file->move($destinationPath,$filename);
$uploadcount ++;
$file->getClientOriginalExtension();
$entry = new \App\Product();
$entry->mime = $file->getClientMimeType();
$entry->filename = $filename;
$entry->save();
}
@zainudinnoori sir it shows
SQLSTATE[HY000]: General error: 1364 Field 'filename' doesn't have a default value
@Shawdow it shows that because you are trying to push a null value into 'filename' column into your table . Are you trying to save a file or images ?
@zainudinnoori sir after redirecting back fill the upload image it shows me FatalThrowableError (E_ERROR)
Call to a member function getClientOriginalName() on string
@Shadow This works fine for me
<form method="POST" action="/image/update" enctype="multipart/form-data" class="box-typical">
{{ csrf_field() }}
<input type="file" id="file-input" class="gallery-photo-add" name="images[]" multiple accept="image/*">
<button type="submit"> Submit </button>
</form>
and in your Controller
if(! is_null(request('images')))
{
$photos=request('images');
foreach ($photos as $photo)
{
$destinationPath = 'images';
$filename = $photo->getClientOriginalName();
$photo->move($destinationPath,$filename);
$uploadcount ++;
$photo->getClientOriginalExtension();
$entry = new \App\Product();
$entry->mime = $photo->getClientMimeType();
$entry->filename = $filename;
$entry->save();
}
@endforeach
}
Hope it works for you.
$FileName = $request->image->hashName();
Try this
@zainudinnoori
Call to a member function getClientOriginalName() on string shows me with same error sir
in the my controller
$prod = new \App\Product;
$prod->name = Input::get('name');
if(! is_null(request('images')))
{
$photos=request('images');
foreach ($photos as $photo)
{
$destinationPath = 'images';
$filename = $photo->getClientOriginalName();
$photo->move($destinationPath,$filename);
$uploadcount ++;
$photo->getClientOriginalExtension();
$entry = new \App\Product();
$entry->mime = $photo->getClientMimeType();
$entry->filename = $filename;
$entry->save();
}
}
$prod->save();
in the blade file
{!! Form::open(array('action' => 'SellerController@inputProduct','method'=>'POST')) !!}
<div class="form-group">
<strong>Product Name:</strong>
{!! Form::text('name', null, array('placeholder' => 'demo', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
<strong>Image:</strong>
<form enctype="multipart/form-data" class="box-typical">
{{ csrf_field() }}
<input type="file" id="file-input" class="gallery-photo-add" name="images[]" multiple accept="image/*">
</form>
</div>
@suraj_jadhav16 same error sir
FatalThrowableError (E_ERROR)
Call to a member function getClientOriginalName() on string
Replace your code
{!! Form::open(array('action' => 'SellerController@inputProduct','method'=>'POST')) !!}
To
<form method="POST" action="/image/update" enctype="multipart/form-data" class="box-typical">
That error is because you are not adding
enctype="multipart/form-data"
in your tag.
@zainudinnoori something error changes next line sir
Call to a member function move() on string
this my solution sir @zainudinnoori thank you sooo much
if(! is_null(request('images')))
{
$photos=request('images');
$uploadcount = 0;
foreach ($photos as $photo)
{
$destinationPath = 'images';
$filename = $photo->getClientOriginalName();
$photo->move($destinationPath,$filename);
$uploadcount ++;
$photo->getClientOriginalExtension();
$prod->mime = $photo->getClientMimeType();
$prod->filename = $filename;
}
}
@zainudinnoori you need to make a small change the code suggested by @Shawdow
replace
$photos = request('images');
with
$photos = request()->file('images');
This should fix the errors like calling move() and getClientOriginalName() on strings.
Hope this helps.
Please sign in or create an account to participate in this conversation.