Laravel - upload multiple files - php

I was searching for some tutorials on how to upload multiple files, for example this: http://tutsglobal.com/discussion/301/how-to-upload-multiple-images-in-laravel-4
There is a {{ Form::file('images[]', ['multiple' => true]) }} line that should make from input for selecting multiple files. But the problem is that I can select only one file and not more. What could I do to be able to upload multiple files?
This is in my View:
{{ Form::open(['action' => 'AdminController#postProject', 'files' => true, 'enctype' => 'multipart/form-data', 'class' => 'projectform']) }}
{{ Form::file('images[]', ['multiple' => 'multiple']) }}
{{ Form::close() }}
This is HTML output:
<input multiple="multiple" name="images[]" type="file">
I just found that I can select multiple files with holding down SHIFT key, but I'd like to be able select files separately, one by one.

Hint: maybe its helpful for you.
<form action="demo_form.asp">
Select images: <input type="file" name="img" multiple>
<input type="submit">
</form>

Related

file input throws an error when form is submitted

I run into a problem when trying to upload images to the server using a symfony form as follows:
$form = $this->createFormBuilder($image)
->add('full', FileType::class, array('label' => 'Imagen', 'multiple' => true))
->add('save', SubmitType::class, array('label' => 'Guardar'))
->getForm();
Then in the html:
{{ form_start(form) }}
{{ form_row(form.full) }}
{{ form_end(form) }}
When I inspect the html I get:
<form name="form" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label required" for="form_full">Imagen</label>
<input type="file" id="form_full" name="form[full][]" required="required" multiple="multiple" />
</div>
<div class="form-group">
<button type="submit" id="form_save" name="form[save]" class="btn-default btn">Guardar</button>
</div>
<input type="hidden" id="form__token" name="form[_token]" value="vZjUzyZCsbsx5TmfWiljncIi1pPymfod_jezOOKgK_k" />
</form>
When I get the value of the file in the controller I have this:
originalName="myPicture.jpg"
mimeType="image/jpeg"
size=8450
erro=0
*SplFileInfo*pathName="E:\xamp\tmp\php51B5.tmp"
*SplFileInfo*fileName="php51B5.tmp"
So far, well, but in other cases of other images with the same format (jpeg), it happens that I get this:
originalName="myPicture.jpg"
mimeType="application/octet-stream"
size=0
erro=1
*SplFileInfo*pathName=""
*SplFileInfo*fileName=""
As you can see it does not recognize the Mimetype that should be image / jpeg, and it shows that an error occurs but it does not say which one?
As RiggsFolly said, the problem is that the error = 1 means that the file you are trying to upload is larger than the limit set in the php.ini of the server, changing this parameter is solved:
php.ini:
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=8M

File could not be uploaded in laravel although all the tags and functions are seemed okay

First question asked in this forum.
I have watched many tutorials on file uploading in laravel, did exactly what they did but file is not uploading. It would be great of you could help me.I am posting all the relevant codes for this.
Here is my html code for taking file input and other inputs
<div id="form" >
<div id="select" style="font-size:20px;">
{{ Form::open(['route' => 'gpa.science'])}}
<div id="youtubelink" style="font-size:20px;">
<p>শিরোনাম :</p>
<h22 > {{ Form::textarea('title', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div id="youtubelink" style="font-size:20px;">
<p>ইউটিউব ভিডিও লিঙ্ক :</p>
<h22 > {{ Form::textarea('videokey', null, ['size' => '70x1']) }} </h22>
</div>
</br>
<div>
<form action="" name="filea">
<input type="file" name="filea" enctype="multipart/form-data">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</form>
<div class="input-filed">
{{ Form::submit('Submit', ['class'=>'btn btn-primary right'])}}
{{ Form::close()}}
</div>
Here is my route
Route::post('/blog1', ['as'=>'gpa.science', 'uses' => 'PageController#blogafter']);
Now after submit button this will go to this PageController.
PageController Code:
<?php namespace App\Http\Controllers;
use DB;
use App\Quotation;
use Input;
use Illuminate\Http\Request;
use App\Filename; use Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\UploadedFile;
class PageController extends Controller {
public function blogafter(Request $request){
//return $request->all();
if($request->hasFile('filea'))
{
dd('Got the file');
}
dd('No file');
return view('blogafter');
} }
Now the problem is it does not get any file. Always shows no file.If I do $request->all(); it returns
videokey null
filea "working.sql"
Now can anyone tell me what is wrong in my code? Why I can not upload files. I am using laravel 5.4.36 and php version 5.6.31
You have typo in your form which is missing enctype="multipart/form-data"
<form action="" name="filea" method="post" enctype="multipart/form-data" >
Add
{{ Form::open(['route' => 'gpa.science', 'files'=> true])}}
Hope this helps.
Change
{{ Form::open(['route' => 'gpa.science']) }}
to
{{ Form::open(['route' => 'gpa.science', 'files' => true]) }}
This will add enctype="multipart/form-data" to the form, which is required to upload files to PHP.

Laravel 5.1 form - 'files' => true

I use Laravel HTML to create form but I have problem, so in creating form I have:
{!! Form::open(['url'=>'vocuhers','files' => 'true','enctype'=>'multipart/form-data']) !!}
#include('vouchers.form',['submitButtonText'=>'Click to Add New Vocuher'])
{!! Form::close() !!}
But when I see my HTML form in browser there is just:
<form method="POST" action="http://localhost:8888/vouchers" accept-charset="UTF-8">
<input name="_token" type="hidden" value="dfgdfgdfgdfgdf">
so where is
enctype="multipart/form-data"
which allow me to upload files from form ?
Why I don't get this HTML output:
<form method="POST" action="https://bedbids.com/chats" accept-charset="UTF-8" enctype="multipart/form-data">
<input name="_token" type="hidden" value="dsfdfgdfgdfgdfg">
What is the problem here exactly ?
Your syntax is wrong. The following works for me:
{{Form::open(array('url' => 'your_url', 'method' => 'post', 'files' => true))}}
Change url with route as below.
{{!! Form::open(['route'=>'vocuhers','class'=>'your_class','files'=>true]) !!}}
{!! Form::open(['url'=>'vocuhers','files' => 'true','enctype'=>'multipart/form-data']) !!}
change It to
{!! Form::open(['url'=>'vocuhers','files' =>true,'enctype'=>'multipart/form-data']) !!}
as Markinson said
Change it to like this:
{!! Form::attributes(['enctype'=>"multipart/form-data"])open(['url'=>'vocuhers']) !!}

Form file upload protected properties

I can not handle file upload forms. Sorry if it is a dummy question, but:
If I use 'files' => 'true' or 'enctype' => 'multipart/form-data' in the Forms open tag I get an object with protected properties. How can I handle the originalName, mimeType etc.. in my app?
To handle file uploads you do:
On your view:
<form action="{{ UR::route('upload') }}" method="POST" enctype="multipart/form-data">
<input type="file" name="photo" />
<input type="submit" value="Upload!">
</form>
Or in blade:
{{ Form::open(array('url' => UR::route('upload'))) }}
{{ Form::file('photo'); }}
{{ Form::submit('Upload!'); }}
{{ Form::close() }}
Then on your controller you can:
$name = Input::get('photo')->getFileName();
$size = Input::get('photo')->getClientSize();
Input::get('photo')->move(public_path().'/uploads', $name);
You can find a full list of methods in the file vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\File\UploadedFile.php

Laravel 4 upload image form

I'm trying to make a form that allows uploading a file and store it in the public/upload folder of the application.
my form is:
<form action="/image/save" method="post">
<input name="image" type="file" />
<input type="submit" value="Save"/>
</form>
and the controller:
public function save(){
$file = Input::file('image');
$destinationPath = 'upload/';
$filename = $file->getClientOriginalName();
Input::file('image')->move($destinationPath, $filename);
But when I run it I get the following error:
Call to a member function getClientOriginalName() on a non-object
You need to have a file input enabled on a form. (Doc)
In Laravel, you can use:
Form::open('image/save', array('files'=> true))
or
<form action="/images/save" method="post" enctype="multipart/form-data">
Otherwise all you are receiving from the file input is the file name as a string.
If you not use Blade for generate your form like this
{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}
Just add this parameter to your form tag
enctype="multipart/form-data"
I also faced the same problem with one of my forms, the problem was the form wasn't defined with the option, 'files' => true , which tells the Laravel4 form helper to define a form with enctype="multipart/form-data"
Here is what i did:
Define the following array in your controller and pass it to your view ,
$form_options = array (
'url' => 'path/to/defined/route',
'class' => 'form-horizontal',
'name' => 'newUser',
'files' => true
);
In your view, define your form as follows:
{{ Form::open( $form_options ) }}
That will define your form with enctype="multipart/form-data"
You have to get the filename as follows:
//Blade form
{{ Form::open_for_files('/image/save') }}
{{ Form::file('image') }}
{{ Form::close() }}
//get imagename
$filename = Input::file('image.name');
Good luck! :)
You can just add the field 'files'=> true in your array() if you use Laravel form style something like
{{ Form::open(array('url'=>'/images/save','role'=>'form','files'=> true)) }}
otherwise you can use the html form tag something like
<form action="/images/save" method="post" enctype="multipart/form-data">
Hope this will help you.

Categories