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
Related
When the user accesses a certain brand page, I pull the information associated with that brand. Then the user has the chance to submit an application for this brand.
When the user submits the form, I want the form to post to /apply/brand/{brand_id} because I want to store this application in my application table with the brand_id as one of the fields (the other fields in this table comes from the fields in my form, but the brand_id will be an URL parameter)
The problem is that when I submit the form, the form posts to /apply/brand/undefined and the submission does not work correctly. I do not reach the ApplicationController#apply_store method.
EDIT:
To debug my problem, I printed out the {{$brand -> id }} right before the element and it printed out fine. However, when the form submits, it goes to /apply/brand/undefined instead of /apply/brand/{{$brand -> id }}. The $brand variable somehow becomes undefined inside of my form.
EDIT:
I hardcoded the from to submit to /apply/brand/43. When I press submit, the url shows up as /apply/brand/43 at first but then quickly changes to /apply/brand/undefined before redirecting me to my default page.
Controller Method for Accessing a Brand Page
public function brandProfile(){
$brand = Brand::where('user_id', Auth::user()->id)->first();
$industry = Industry::where('status', 1)->get();
return view('new-design.pages.profile_brand')
->withData($brand)
->withIndustry($industry);
}
Brand Application Form
<form id="application_form" method="post" action="/apply/brand/{{ $data -> id }}" enctype="multipart/form-data">
{{ csrf_field() }}
<ul>
<div class="col-md-6">
<li>
<label>First Name</label>
<input type="text" class="form-control" name="firstname" placeholder="First Name"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Last Name</label>
<input type="text" class="form-control" name="lastname" placeholder="Last Name"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email"/>
</li>
</div>
<div class="col-md-6">
<li>
<label>Instagram Handle</label>
<input type="text" class="form-control" name="instagram" placeholder="Instagram Handle"/>
</li>
</div>
<li>
<label>Cover Letter</label>
<p>Please write your message in the space below, or attach a file (-list of file types accepted-)</p>
<textarea cols="30" rows="50" name="message" class="textarea"></textarea>
</li>
<li>
<div class="upload-cover-letter">
<i class="fa fa-paperclip" style="cursor:pointer;font-size:20px;"></i>
<input type="file" name="file" id="myFileDocument" class="inputfile inputfile-1"/>
<label for="myFileDocument" id="myFileDoc"><span>Choose File</span></label>
<span style="font-size: 12px">No File Chosen</span>
<span class='hidden_text' style="font-size: 12px">Upload File (Max 2MB)</span>
</div>
<input type="hidden" id="myFileName" name="file_name" />
</li>
</ul>
<div class="btn-center">
<button type="button" class="btn btn-gradient waves-effect" id="create_campaign">Apply Now</button>
</div>
</form>
Route in web.php
Route::post('/apply/brand/{brand_id}', 'ApplicationController#apply_store');
Store application in database
public function apply_store(Request $request)
{
$application = new Application([
'influencer_id' => Auth::id(),
'brand_id' => $request->get('brand_id'),
'message' => $request->get('message'),
'status' => 'applied'
]);
$application->save();
// TODO: add helper message to confirm application did return
return redirect('/apply');
}
In your controoler metohd apply_store, you need to put the variable that will receive the variable sended by url parameter.
public function apply_store(Request $request, $brand_id){}
I typically work with compact or with to send the param to the blade view. So:
return view('new-design.pages.profile_brand', compact('brand'));
or without compact:
return view('new-design.pages.profile_brand')->with('brand', $brand)
I haven't seen the withVar that you are attempting above (doesn't mean it doesn't exist though). Try with compact and dump $brand on the view to make sure its coming through with data (not undefined). If that dumps successfully, but still fails, you may want to try adding the variable outside the quotes or totally within the blade {{}} in the form like:
<form id="application_form" method="post" action={{ "/apply/brand/".$brand-> id }} enctype="multipart/form-data">
Not sure about how the action is getting though like you have in your code above, though - you might wish to use the url() method:
<form id="application_form" method="post" action={{ url("/apply/brand/".$brand-> id) }} enctype="multipart/form-data">
change your method like this
public function apply_store(Request $request,$brand_id)
{
$application = new Application([
'influencer_id' => Auth::id(),
'brand_id' => $rbrand_id,
'message' => $request->get('message'),
'status' => 'applied'
]);
$application->save();
// Ngentod lah kalian semua, anjeng
return redirect('/apply');
}
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.
In a view in laravel i have a form that upload a file:
<form id="file-submit" enctype="multipart/form-data" method="post" action="store">
{{ Form::token() }}
<input id="filename" name="filename" type="file" />
<input type="submit" value="Upload file" id="file-save" class="btn btn-create" />
</form>
In the route file i have:
Route::post('/store', 'MyController#upload');
There in the method, i process the file data.
This works in my local server, i upload it in production, so, this works when i upload files around 5kb, but if i try to upload a large file, around 4MB it breaks with this error:
production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in <laravel_instance>/protected/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
I have the same memory settings in php.ini file (memory_limit, post_max_size, excecution_time, max_input_time, upload_max_size)
Thanks
I suspect that it has something to do with you manually creating the form.
Try doing it like this:
Route:
Route::post('/store', [
'as' => 'store',
'uses' => 'MyController#upload'
]);
Form:
{{Form::open([
'id' => 'file-submit',
'enctype' => 'multipart/form-data',
'action' => 'store'
])}}
{{Form::file('filename')}}
{{Form::submit()}}
{{Form::close()}}
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>
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