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));
Related
I want to delete these files in my laravel like this. But it doesnt work!!
use Storage;
class SomeController extends Controller
{
public function delete()
{
$filesToDelete = [
'http://minio:9100/minio/myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'http://minio:9100/minio/myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
// cant delete using this
Storage::disk('s3')->delete($filesToDelete);
}
}
But when I try to delete using this command in my bash it works perfectly
aws --endpoint-url http://minio:9100 s3 rm s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
delete: s3://myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg
my aws configure list is exactly the same in my project config!
I can use Storage to upload the files but when I try to delete the files it doesnt work:
// Working
Storage::disk('s3')->put('1/ImNkE0wwrstgSpzFyruw8.jpeg', $file);
Am I doing something wrong? Is there anyway to delete these files using laravel??
https://docs.min.io/docs/aws-cli-with-minio
https://readouble.com/laravel/5.4/en/filesystem.html
I think the problem is file path. Try to run as mentioned below.
Storage::disk('s3')->delete('myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg');
I solving this parsing url, like this:
$file_path = parse_url($file_url);
//return $file_path['path'];
Storage::disk('s3')->delete($file_path);
it works for me.
In your bucket, the files has a key, thath be need to delete file from your php function. to get this the object url be parsed.
You can do it by this way
$file_name='your_file_path';
unlink(storage_path($file_name));
You are adding wrong file path.In order to remove image form s3 bucket you are not need to give full path of image, Just add root path of image.
To delete multiple images
$files = [
'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg',
'myapp/1/YFrdE0sarsAcpfFyrifd2.jpeg'
];
Storage::disk('s3')->delete($files);
Delete single image
$file = 'myapp/1/ImNkE0wwrstgSpzFyruw8.jpeg';
Storage::disk('s3')->delete($file);
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.
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
I am trying to import a csv file with Laravel 5.5 from the local file location. For some reason it can't find the file on my computer however the path is correct.
$fileLocation = $request->file('file')->store('csv');
$importFile = File::file(storage_path('app/' . $fileLocation));
You can use the Storage facade for this. Here is an example:
if (Storage::disk($disk)->exists($filename)) {
return Storage::disk($disk)->get($filename);
}
throw new FileNotFoundException(sprintf('File not found: %s', $filename), 404);
If you don't wanna use Storage facade, you can use below code to get the file
$importFile = Illuminate\Support\Facades\File::get(storage_path('app/' . $fileLocation));
but when you store a file with csv as store argument, it will save in storage/csv folder, and you just need call storage_path($fileLocation).
be sure that your storage path is correct and for more information read here
I have a .txt file with countries and their codes and I want to get the contents from it and insert into the database.
But when why try to open the file using the php function fopen() it throws maximum execution time error
here's the code:
web.php:
Route::get('/countries', 'PageController#insertCountries');
PageController:
public function insertCountries()
{
$file = fopen(asset('databases/countries.txt'), 'r');
return 'ok';
}
The size of the file is 6KB. I am using Laravel 5.4
EDIT: the file is in mu public folder in folder databases
If you want to open local file, use File facade to work directly with filesystem, also you shouldn't use asset() helper. So, do something like this instead:
$file = File::get('/full/path/to/the/file/countries.txt');