I have button upload for uploading PDF in my view, the file was saved in database.
Here is my view html:
<div class="form-group">
{!! Form::label('file', 'File:') !!}
<p>{!! $attatchment->file !!}</p>
</div>
So I want to save it on my local folder, how to make its works ?
I'm using this: path="/file_storage";
$file = $request->file('avatar');
$destinationPath = 'file_storage/';
$originalFile = $file->getClientOriginalName();
$filename=strtotime(date('Y-m-d-H:isa')).$originalFile;
$file->move($destinationPath, $filename);
it will save the file in public/file_storage
<?php
$file = $request->file('photo');
$time = microtime('.') * 10000;
$filename = $time.'.'.strtolower( $file->getClientOriginalExtension() );
$destination = 'profile';
$file->move($destination, $filename);
?>
Related
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'));
}
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).
I have been trying to upload multiple files in Laravel using the code below but it only uploads a single image. Please help
$files = $request->file('file');
foreach ($files as $file){
$filename = time().'.'.$file->getClientOriginalExtension();
$location = public_path('uploads/'.$filename);
$file->move(public_path().'/uploads/', $filename);
$filename_arr = [];
array_push($filename_arr, $filename);
$filename = json_encode($filename_arr);
$upload->filename = $filename;
}
Blade: As you want to upload multiple file append [] to the input type name property with multiple as below :
<input type="file" name="file[]" multiple>
Logic:
if($request->hasFile('file'))
{
$files = $request->file('file');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$file->move(public_path().'/uploads, $filename);
}
}
how can i upload inputs files in different folders i need to use switch cases to make every input file in different folder and did this code but when i execute it nothing happen i want know where problem
my view
{!! Form::file('file1', null,['class'=>'form-control']) !!}
{!! Form::file('file2', null,['class'=>'form-control']) !!}
{!! Form::file('file3', null,['class'=>'form-control']) !!}
{!! Form::file('file4', null,['class'=>'form-control']) !!}
my controler
$model = new Files($request->all());
switch ($model) {
case "file1":
if ($request->hasFile('file1')) {
$file = $request->file('file1');
$destinationPath = public_path() . '/file1';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file1'] = $filename;
$model -> file1 = $filename;
$model->save();
}
break;
case "file2":
if ($request->hasFile('file2')) {
$file = $request->file('file2');
$destinationPath = public_path() . '/file2';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file2'] = $filename;
$model->file2 = $filename;
$model->save();
}
break;
case "file3":
if ($request->hasFile('file3')) {
$file = $request->file('file3');
$destinationPath = public_path() . '/file3';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file3'] = $filename;
$model->file3 = $filename;
$model->save();
}
break;
case "file4":
if ($request->hasFile('file4')) {
$file = $request->file('file4');
$destinationPath = public_path() . '/file4';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
$request['file4'] = $filename;
$model->file4 = $filename;
$model->save();
}
break;
}
Ummm yeah I had do something like that, but I do it use foreach loop and if after that.
And here's my sample code, I hope it can help :-D
$requests = $request->all();
$model = new File;
foreach ($requests as $key => $val) {
if ($key == 'file1' && !empty($val)) {
$destinationPath = public_path() . '/file1';
$filename = $val->getClientOriginalName();
$val->move($destinationPath, $filename);
$model -> file1 = $filename;
$model->save();
}
if ($key == 'file2' && !empty($val)) {
$destinationPath = public_path() . '/file2';
$filename = $val->getClientOriginalName();
$val->move($destinationPath, $filename);
$model -> file1 = $filename;
$model->save();
}
// And do it again as much as you need :D
}
Here you go, I hope it can help you pals :D
I am new to Laravel 5. Actually I'm using Laravel Framework version 5.1.16. When uploading an image, it is uploading and saved in database table, but it is not moved to a specified folder.
Any help would be appreciated.
This is my controller code:
public function addexpense(Request $request) {
$inputs=Input::all();
$validation = Validator::make($inputs, Expense::$rules);
if($validation->fails()) {
return Redirect::to('expenseaddform')->withErrors($validation)->withInput();
}else{
$input=Input::only('id','expense_date','expense_category_id','vendor_id','customer_id','amount','tax1_id','tax2_id','note','receipt');
$data->expense_date = $input['expense_date'];
$data->expense_category_id = $input['expense_category_id'];
$data->vendor_id = $input['vendor_id'];
$data->customer_id = $input['customer_id'];
$data->amount = $input['amount'];
$data->tax1_id = $input['tax1_id'];
$data->tax2_id = $input['tax2_id'];
$data->note = $input['note'];
$image = Input::file('receipt');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/uploas/'. $filename);
$data->receipt = $input['image'];
$data->save();
return redirect('expenseinfo');
}
}
Form code:
<form action="addexpense" enctype="multipart/form-data" >
Receipt:</td><td><input type="file" name="receipt"value="{{Input::old('receipt')}}">
<input style="margin-left:30px"type="submit" value="Add" name="submit">
</form>
You are doing it wrong with Image::make() function. Try the following:
Image::make($image->getRealPath())->resize(468, 249)->save(public_path() . '/uploads/' . $filename);
Try this:
Image::make($image->getRealPath())->resize(468, 249)->save(public_path('uploads/'). $filename);
For more information you can check the below link:
https://youtu.be/F92JpVWhDw4