Laravel 5.5 read a file from the local disk - php

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

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

Laravel Storaget get file contents

I want to know if you do the following in my code below how do you check if the file exists e.g
{
$file = Storage::disks('local')->get($file);
// then test if $file exists
}
You can check the existence of the file with the code below for your local storage,
$file_exists = Storage::disk('local')->exists($file);
You can read more about storage on Laravel Document at Laravel Filesystem.
Note: in the document you will check disk('s3'). It depends on what connection we are using defined in config/filesystem.php(the configuration file for the filesystem in laravel).
OR simply you can use check the condition by
if(Storage::disk('local')->exists($file)) {
}
It will also work for you.
There's a clear example in the docs, if you take a look.
$exists = Storage::disk('s3')->exists('file.jpg');

Laravel maximum execution time error when trying to open .txt file

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

Write on YAML file using php in codeigniter Framework

I'm using YAML library with codeIgniter and I was adding data manually but now I decided I need to add it automatically and I'm asking how to add a line at the end of a .YML file?
Clarification:
when I insert some data in the database in the same time I need to store some of it in the YML file ( for other use )
no the data is inserted successfully in the database but it doesn't in the YML file (it always return Unable to write the file) after I tried to use this method which I thought it's gonna work:
$title = $title-en.' :'.$title-fr;
$this->load->helper('file');
$msg = "non";
if (!write_file(base_url().'assets/data/data.yml',$title, 'a+')){
$msg = 'Unable to write the file';
}else{
$msg = 'File written!';
}
echo $msg;
You cannot write to a file using a URL ie http://www.domainname.com/assets/data/data.yml
You need to use a PATH. ie
if (!write_file('.assets/data/data.yml', $title, 'a+')) {
...
}
So if you use the correct path and the file has it's permissions set to writeable, that should help.

Categories