How to download file from storage? - php

I'm using Laravel's file storage system and I'm trying to trigger a download response to download my files through the browser, but it cant find the correct file instead it downloads my file page view script. I have a storage link set up as well.
Any suggestions would be appreciated.
File.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file)}}" download="{{$file->name}}">
{{$file->name}}</a>
</div>
</div>
#endsection
Download function
public function download($file){
return response()->download(storage_path('/storage/app/files/'.$file));
}
file routes
Route::get('files', 'FileController#index')->name('upload');
Route::post('files', 'FileController#store');
Route::get('files/{file}', 'FileController#download')->name('download');

Remove this download="{{$file->name}}" from the link.
You can add download as html attribute:
<a href="{!! route('download', $file->name) !!}" download>{{ $file->name }}</a>
But you don't need it in this case, use just:
{{$file->name}}
The response()->download() method in your controller will generate a response that forces the browser to download the file at the given path. So make sure your path i correct.
If your file is in your-project-root-path/storage/app/files/, you can use:
return response()->download(storage_path('/app/files/'. $file));
If your file is in your-project-root-path/storage/storage/app/files/, use:
return response()->download(storage_path('/storage/app/files/'. $file));

I think you are passing the file object instead of the filename to your download route.
Try
#extends('layouts.app')
#section('content')
<div class="container">
<form action="{{route('upload')}}" method="POST"
enctype="multipart/form-data" name="formName">
{{csrf_field() }}
<input type="file" name="file">
<input type="submit" class="btn" name="submit">
</form>
<div class="row">
#foreach($files as $file)
<a href="{{route('download',$file->name)}}" download>
{{$file->name}}</a>
</div>
</div>
#endsection
Try replacing {{route('download',$file)}} with {{route('download',$file->name)}}.
Also try replacing the download controller with this code
public function download($file){
return response()->download(storage_path('app/files/'.$file));
}

public function jpg_download($id)
{
if (auth()->user()->download_count < 5) {
auth()->user()->increment('download_count');
$data = DB::table('products')->where('id', $id)->first();
$path = public_path('/storage/item/jpg/' . $data->jpg);
return response()->download($path);
}
dd('Next Day Download');
}

Related

Why doesn't my php laravel application store my image?

I've looked at code from other people and try to implement the same thing, but the application could not store the image to the desire folder, would you please have a look at what the reason is? Thank you!
Here is my code for route:
Route::post('upload_pic', 'UploadController#storePhoto');
Here is my code for the php laravel template:
<div class="panel-body">
<form action="{{url('/upload_pic')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="upload-user-photo">Upload Photos</label>
<input type="file" name="image" class="form-control" placeholder="Upload Student Image, Size:207(W)x408(H)">
</div>
<div class="row">
<div class="col-sm-4">
<input class="btn btn-success" type="submit" value="Upload Student Photo" name="submit">
</div>
</div>
</form>
</div>
Here is my code in the controller:
public function storePhoto(Request $request){
$valid = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,|dimensions:max_width=272,max_height=408,min_width=271,min_height=407'
]);
$data = new Postimage();
$file = $request->file('image');
$filename = $file->getClientOriginalName();
// dd($filename);
$file -> move(public_path('./public/img/student_photos'),$filename);
$data['image'] = $filename;
$data->save();
return redirect('/upload')->with('success', 'Photo Uploaded');
}
Your problem is somewhere here
$filename = $file->getClientOriginalName();
$file -> move(public_path('./public/img/student_photos'),$filename);
First, the file comprises only the file extension. Instead, you should have something like this $filename = 'name.' . $file->getClientOriginalName(); Notice the dot in 'name.'
Secondly, no need to add public to the file path string. So it should be $file->move(public_path('img/student_photos'),$filename);
Finally, make sure the upload folder exists and is writeable

move_uploaded_file is not fetching path in laravel

Here is my controller
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
move_uploaded_file($tempname, 'picture/banner/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
here is my route
Route::post('/BannerEdit', [App\Http\Controllers\BannerController::class,
'createAdmin']);
here is my blade form
<form action="{{ url('') }}/BannerEdit" method="post" class="col-12"
enctype="multipart/form-data">
#csrf
<div class="mb-3 col-12">
<label class="col-4 text-label form-label">Banner Photo*</label>
<input type="file" name="bannerPic" class="form-control input-rounded col-4 mb-3"
required>
</div>
<div class="mb-3 text-end">
<input type="submit" class="btn btn-primary" value="Upload">
</div>
</form>
When i submit the data it Gives me an Error as
move_uploaded_file(picture/banner/favicon.jpg): Failed to open stream: No such file or directory
But this Directory Exists
And i checked with full pathof localhost then it does not supports http path
You need to use getcwd() function like this:
$dirpath = realpath(dirname(getcwd()));
So your controller code will be:
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
$dirpath = realpath(dirname(getcwd()));
move_uploaded_file($tempname, $dirpath.'/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
let me know if it works.. if not then we'll modify $dirpath variable
PS: this solution will work on server. are you working on server or in localhost?
EDIT:
Other solution is to use below function to get proper directory structure:
$dirpath = public_path('picture/banner/');

Get the actual file name and extention from image file in laravel

When I am uploading an image to the form and returning it from the controller, the image name and extention are changing.I am a beggar, so if I make a mistake while asking a question, I would like to apologize.
This is my form:
<form action="{{ route('admin.slider.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-12">
<label class="control-label">Image</label>
<input type="file" name="image">
</div>
</div
<button type="submit" class="btn btn-success">Save</button>
</form>
This is my controller:
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,bmp,png,jpg',
]);
$image = $request->file('image');
return $image;
}
My image file name is :demo.jpg
Controller return result is like that:
C:\xampp\tmp\php5E86.tmp
This is the same result when I give another picture, only the last four characters are changing.
C:\xampp\tmp\phpF239.tmp
It is very helpful to know why I am getting .tmp file return.
use getClientOriginalName to get orginal file name
$request->image->getClientOriginalName()
To get file extension
$request->image->extension();
or
$name = $request->file('image')->getClientOriginalName();
$extension = $request->file('image')->extension();
Ref:https://laravel.com/docs/8.x/filesystem#other-uploaded-file-information

Call to a member function getClientOriginalName() on null laravel

I keep on getting this error when I try to upload a pdf document file, does anybody have any idea how to solve this?
I had tried looking for similar problems but still can't solve it(eg: Upload pdf file using Laravel 5)
I tried doing dd() to see if the file have been uploaded and it did show the file name but the error said Call to a member function getClientOriginalName() on null, so now I'm kind of confused on what to do now.
Here are my codes, thanks in advance for helping.
Controller:
class CreateController extends Controller
{
public function create(){
return view('create');
}
public function store(Request $request){
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension();
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
//dd($request);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
create.blade.php
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('/user')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
Route:
Route::get('/user/create','CreateController#create');
Route::post('/user','CreateController#store');
This enctype solved function call on the null problem
<form method="" action="" enctype="multipart/form-data">
You need to use the file() function to have access to your uploaded file
$request->file('upload_file')
use this code in your tag form enctype="multipart/form-data"
<form method="" action="" enctype="multipart/form-data">
Try to put the file into a var.
$file = $request->file('upload_file');
And get the extension and name from it.
$uniqueFileName = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
Hope it helps.
The key to solving this problem is that you must write getClientOriginalName() function in the if clause just like that:
if($request->hasFile('logoImage')){
$logoImage = $request->file('logoImage');
$name = $logoImage->getClientOriginalName();
}
if($request->has('sound_file')) {
$image = $request->file('sound_file');
$filename = $image->getClientOriginalName();
request()->$image->move(public_path('images/users'), $filename);
$sounds->image = $request->file('sound_file')->getClientOriginalName();
}

homestead laravel 5 : link Download file from storage

i am need hint link to download upload file, currently i am following this answer
here is my code
Routes file :
Route::get('getDownload/{remind_letter}',
['as'=>'downloadData',
'uses'=>'DockController#getDownload']);
Controller File :
public function getDownload($remind_letter)
{
$download = Dock::where('remind_letter','=',$remind_letter)->firstOrFail();
$file =Storage::disk('local')->get($download->remind_letter);
return (new response($file))->header('Content-Type', $entry->m1);
}
result file
<div class="row">
<div class="form-group">
<div class="col-xs-5">
<label class="col-sm-7">Remind Letter :</label>
<input type="text" name="remind_letter" value="{{$getData->remind_letter}}">
</div>
<div><a href="{{ route('downloadData',$getData->remind_letter) }}">
<button class="btn btn-success btn-sm">Download</button></a></div>
</div>
</div>
and the result link dock.start/star/getDownload/home/vagrant/Code/dock/storage/app/25/JTK/2015/Renewal License Kaspersky/Moonarch Security/tmp/phpQaGzoD.pdf
but the link is always show up error 404 and i can't download file
FYI when upload file i am using storage_path() function
it fix, i am wrong logic, when upload file it should using getFilename() function, $entry->filename = $filename->getFilename().$extension;

Categories