How to get name of stored files in public - laravel 5 - php

When i upload my files to the server, it works fine and displays in my public folder as img/php786W. I am currently trying to retrieve my folder but i get file not found on server.
In my controller, $filename is returning the original name of the file on the server. So it does not match to be found although it is the same file.
How can i get this done? I tried to use hashName() but that won't work
Controller
$file_path = public_path() . '/img/'. $filename;
if (file_exists($file_path->hashName()))
{
return Response::download($file_path, $filename, [
'Content-Length: '. filesize($file_path)
]);
}
else
{
// Error
}

Use
return response()->download($pathToFile);
If this does not work you have either permissions problem, or the file simply does not exist
See docs

Related

Laravel 6 Storage results in a 404 error when trying to fetch files

I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.

Downloading File error in laravel

I have used local storage for file storing. I have created table of file, in which title, path, card_id rows stored.
$request->file('file')->storeAs('public/upload',$files->getClientOriginalName());
i.e:passport.png stored at /home/dhruv/MyProject/storage/app/public/upload/passport.png
Above works fine but I'm getting error while downloading file.
in controller...
public function getDownload($file_id)
{
$file = File::find($file_id);
//print_r($file);
return response()->download($file->path);
/* return response()->download(
$file->path,
$file->name,
[],
'inline'//attachment
);*/
}
I tried many downloading methods but any didn't work fine.
I am getting this error.
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException:
The file
"/home/dhruv/MyProject/storage/public/upload/passport.png"
does not exist in file
/home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php on
line 37
I dont know why it is searching at /home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php since i have inserted correct path.
So suggest any method for downloading ..
return response()->download(Storage::get($file->path));
Import this before:
use Illuminate\Support\Facades\Storage;
Laravel Storage
Or try this:
return response()->download(storage_path($file->path));

How Do I return a file for download in Laravel

I need to be able to give the user a file for download, and so I created a route (unless I can do the link for a specific user's CV in blade). My files are stored in storage/app/cv/ directory. I created a route as such:
Route::get('test', function() {
$destinationPath = config('app.CVDestinationPath') . "/static_file.pdf";
$uploaded = Storage::get($destinationPath);
return $uploaded;
}
However this route is giving me a weird output. I think it is the file but it is converting the file to html or something. Could anyone help me return the file (it could be pdf, doc, or docx, or plain text). My file is not stored in the public directory!
Because the file is not uploaded in the public directory, you are required to use a storage method for accessing it first before you return a response.
$destinationPath = config('app.CVDestinationPath') . "/static_file.pdf";
$uploaded = Storage::get($destinationPath);
return (new \Illuminate\Http\Response($uploaded))->header('Content-Type', 'application/pdf');
Make sure to return a header with the content type otherwise the file will not be in an acceptable format.
you can use "Response" to help you with the download headers:
<?php
use Illuminate\Support\Facades\Response;
Route::get('test', function() {
$destinationPath = config('app.CVDestinationPath') . "/static_file.pdf";
return response()->download($destinationPath);
}
?>
Checkout this link https://laravel.com/docs/5.5/responses#file-downloads to find out more e.g. response->file($destinationPath) forces the browser to display the file (.pdf in your case) to the user using its lokal plugins.

PHP can't access newly created zip file

Im using Laravel 5.2 and Zipper to create ZIP archive in a fly and download it by user. I think this problem is generall not strictly related to Laravel or Zipper. Steps:
User click link download_all.
First php script create archive.
Next the same script push created file to force user download.
Everything sounds normal but I have strange behaviour that after zip archive is created (point 2) php/server cant see this newly created file.
Both filesize and file_exists on $filePath return false but file exist. I cant read file why?
When I redirect user to $filePath (instead of reading it and pushing to download) everything is okay and user get file. But why I cant access newly creatd file during "script lifetime"? $paths are correct.
Tested on Windows 7 and Unix.
Any idea?
code:
public function downloadAll($id)
{
$sourceFilesDir = 'upload/source/' . $id;
$zipPath = 'upload/source-zip/' . date('Y-m-d-H-i-s') . '.zip';
Zipper::make($zipPath)->add($sourceFilesDir);
$fullPath = public_path($zipPath);
// works
// return response()->redirectTo($zipPath);
$headers = [
'Content-Type: application/zip',
'Content-Transfer-Encoding: Binary',
'Content-Length: ' . filesize($fullPath),
];
// dont works, cant see file
return response()->download($fullPath, basename($zipPath), $headers);
}
Soved by #Holger.
Zipper should be closed to save file.
Proper code:
Zipper::make($zipPath)->add($sourceFilesDir)->close();
->close()
Unfortunately in Zipper docs this is not clearly mentioned.

Laravel 5.1 - Display images stored in Storage folder

I am making a image uploading function for a website using Laravel 5.1. Because of security problems, I want to store those images in /storage/app folder, instead of /public. Image's URL, for example:
/storage/app/images/<image_name>
In order to get image from /storage/app, I set up a specific route for this:
http://app.com/image/<image_name>
To point image requests to /storage:
Route::controllers([
'image' => 'Frontend\ImageController',
]);
In ImageController, here is the code to create response:
public function missingMethod($filename = [])
{
if (is_array($filename)) {
$filename = implode('/', $filename);
}
// storage_path('app') - path to /storage/app folder
$path = storage_path('app') . '/' . $filename;
$file = \File::get($path);
$type = \File::mimeType($path);
return \Response::make($file,200)
->header("Content-Type", $type);
}
My problem is, after using this route to retrieve images, I can't display them in my view. There were no 404, 500 or any error status codes, request URL was it supposed to be, but it just showed a broken image icon. Is there anything wrong with my code?
Thank you!
Sorry guys, a php scripts in my /config folder has leading space, and that caused the http response failed ## Such a nonsense mistake... Thank you all for your help!

Categories