homestead laravel 5 : link Download file from storage - php

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;

Related

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/');

VIEW PDF 404 LARAVEL 7

I want to have a button where a client can view an applicant's document that the candidate has uploaded, but for some reason I cannot figure out why it doesn't work.
It was working fine offline, but I deployed the site via FTP today and It doesn't want to work.
I am less than a junior developer so please be kind about messy code,
Here is my code for
upload blade:
<form action="{{route('cv')}}" method="POST" enctype="multipart/form-data">#csrf
<div class="card">
<div class="card-header">Update CV</div>
<div class="card-body">
<input type="file" class="form-control" name="cv"><br>
<button class="btn btn-success float-right"
type="submit">Update</button>
#if($errors->has('cv'))
<div class="error" style="color: red;">{{$errors->first('cv')}}</div>
#endif
</div>
</div>
Code for route:
Route::post('user/cv','UserController#cv')->name('cv');
Code for Controller:
public function cv(Request $request){
$user_id = auth()->user()->id;
$cv = $request->file('cv')->store(public_path('storage/files'));
Profile::where('user_id',$user_id)->update(['cv'=>$request->file('cv')->getClientOriginalName()]);
return redirect()->back()->with('message', 'CV Successfully Updated!');
}
Code for viewing the CV :
View my CV
When I click the button I get this url:
cvroad.co.za/storage/Kevin%20Breed.pdf
The name of the file is : Kevin Breed.pdf and that is how it is seen in my database aswell.
but still 404 not found?
Any and all help would be appreciated, thank you!
Instead of hard-coding the file path, use Laravel's storage_path() to get to the right path.
$cv = $request->file('cv')->store(storage_path('files'));
Though if you're using Storage::url(), then it's looking in public/storage, so the proper way to go about it might to use the public_path
$cv = $request->file('cv')->store(public_path('storage/files'));
Also when saving to the database, you're saving the full path, not just the name. You can store just the name like this:
Profile::where('user_id',$user_id)->update(['cv'=>$request->file('cv')->getClientOriginalName()]);

How to download file from storage?

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');
}

Laravel 5.4 - how to put file in the database directory

I'm working on the database restore menu for an application I created, I'm using sqlite for my database, and I want to put my previously backed up database file to the database directory
backupAndRestore.blade.php
<form method="POST" action="/admin/backupAndRestore/restore" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="file-field input-field">
<div class="btn">
<span><i class="material-icons">file_upload</i></span>
<input type="file" name="database" accept=".sqlite">
</div>
<div class="file-path-wrapper">
<input type="text" class="file-path" placeholder="Example: file_name.sqlite" readonly>
</div>
</div>
<button type="submit" class="btn">restore</button>
</form>
web.php
Route::post('/admin/backupAndRestore/restore', 'DatabaseController#restore');
DatabaseController.php
public function restore(Request $request)
{
$database = $request->database;
$file_name = 'database2.sqlite';
$path = database_path($file_name);
file_put_contents($path, $database);
return $path;
}
my code works, the file is stored in the database directory, the problem is when I backup my database file size is 22kb, but when I restore it and I check the database directory, the file is 1kb size and when I open using sublime file contents as shown below
Images
which I expect as shown below
Images
can someone tell where my fault is?
You are using file_put_contents which inserts the contents in the given file and does not move the file.
If you want to store a file in database path..
public function restore(Request $request)
{
$this->validate($request, [
'database' => 'required|file|mimes:sqlite'
]);
$databaseFile = $request->database;
$fileName = 'database2.sqlite';
$databaseFile->move(database_path(), $fileName);
// return success message...
}
For more information uploading files: https://laravel.com/docs/5.4/requests#files.

Download link for files in storage not working - Laravel

I am putting my files in storage, and it is working. However, the download link is returning an error.
Here is the view which allows users to upload the files
<div class="form-group{{ $errors->has('notes') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">Upload your notes</label>
<div class="col-md-6">
<input type="file" class="form-control" name="notes">
#if ($errors->has('notes'))
<span class="help-block">
<strong>{{ $errors->first('notes') }}</strong>
</span>
#endif
</div>
</div>
Here is my handling of the request in a controller.
Storage::put(
'notes/' . $note->id . '.pdf',
file_get_contents($request->file('notes')->getRealPath())
);
Everything if firing and the pdf files are being stored in my storage/app/notes directory.
The link I am using to download the files is: /notes/90.pdf, where '90' is the note id.
I have been getting the error
NotFoundHttpException in RouteCollection.php line 161:
You will need to define a route that can serve your file.
Route::get('notes/{id}', function ($id) {
return response()->download(storage_path('notes/' . $id . 'pdf'));
});
You might want to move the download logic to a controller. And add validations to check if the file exists or not but you get the basic idea.

Categories