Laravel How to define a file location on .env? - php

I have a .json file that I want to define on .env.
How do I do that ?
my json file is on (laravel root folder)/json_google/api_key.json .
I tried GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=/json_google/api_key.json but doesn't work ..

Laravel has many helper functions to resolve the path. Such as base_path() in your case. You'll find all helpers here: vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
base_path(env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION');
However, I'd set the full path instead of a relative path. Otherwise your application has to know what it should be relative to (laravel root, or app path, ...) and take this into account, which kind of defeats the purpose of putting the file location outside your code base, as it makes it harder to move it to some place else.
As the .env file is per deployment, there is no problem with setting the full path.

You should use .env variable using constants file.
.env file
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=/json_google/api_key.json
Now in config/constatns.php set
return [
'STORAGE_GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION' => env("GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION", ""),
];
Fetch json file path
Config::get('constants.STORAGE_GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION')

Related

Laravel 5.3 Filesystem store/putFile not working

I am having a problem using the filesystem in Laravel 5.3. I am trying to save a file to my local storage but when I call either $file->store('directory', 'local') or Storage::putFile('directory', $file) it stores in the correct location storage/app/directory/filename but the path returned by both functions does not include the path to the storage/app directory, just directory/filename.
My local storage driver config is as below:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
Obviously I could wrap the $path with the storage_path() helper but this doesn't seem right when reading the docs.
https://laravel.com/docs/5.3/filesystem#storing-files
Am I doing wrong or missing something?
it's not actually crazy. The storage path is configurable, therefore if Laravel were to store it in database but you changed it down the line, there would be an issue.
What you can do simply is add storage_path($file) when you try to display your file in your blade template. That will automatically apply the proper path to storage before the file name and should find your file where it is.
Tell me if that helped/worked
This is by design because in your config, you've already set the storage/app directory as your root directory.
So, it returns the path relative to your root.
Take the case that you want it to return absolute rather than relative paths. Now imagine if your Storage driver was an AWS S3 bucket. In this case there is no absolute path. The absolute path is a term specific to the local driver but the code was written to be extended/used by multiple drivers (and hence they wanted a consistent return value for the putfile method). So, relative path was the only choice in this case

Where to put my .txt files for PHP to read them in Laravel

I am creating a small application that is going to read .txt files and put each row of text in the database. I am not sure where to put those .txt files so that it's in accordance with Laravel standards. Should I put those files in the /public/ directory and access them from a controller via public_path() helper function?
The documentation seems to omit that information. I'd appreciate any help.
When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt
Laravel Configuration

Laravel - Using allfiles gives back empty array

I've been using laravel for nearly all my projects and I've came across a problem when trying to list all files in a directory.
Here is my code below:
$directory = ("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);
So as you can see First line is the directory which is in Upload followed by the username of the account.
The second line is meant to scan this directory and list only files. Sadly it always gives me back an empty array. Using Scandir() here works... But it gives me folders which I'm not wanting.
So... Any tips? I checked the API and I really can't see what is wrong here :(
The reason you're getting no results when using the Storage::allFiles('directory') method is this method only looks inside of your application's storage folder, usually storage/app. Hence why modifying the storage path in configuration works as mentioned by #yassine (you shouldn't do this).
If you wish to use files outside of the storage folder, simply use the File facade instead. e.g. File::allFiles('directory')
Me too i had the same issue for local storage. I fixed it by :
project/config/filesystems.php
line 44
changed root folder to from storage_path('app') to changed to
base_path()
then I provided a related path to the root folder in Storage::files() or Storage::allfiles().
Like Storage::allfiles("app") if I want to fetch files withing the sub folder "app" relative to the base path given in the config file.
Good luck
$direcotry has to be an absolute path from your laravel root folder.
When your uploaded folder is in the root folder try this
$directory = base_path("upload/"."$username->username");
$scanned_directory = storage::allfiles($directory);

How to target database connection file once in PHP?

My PHP application has different config files with their different settings and their different paths. But all of them need to access to just one database connection file. I would like to avoid mention its path separately on them with many ../ and ../../. How can I write a same code and use it in all of them?
Imagine that my PHP application has this hierarchy for its config files:
root
root/config/db.php (This file is what I want to mention from all config files.)
root/admin/config/conf.php
root/users/conf.php
Actually what I need is something that detect the root path of my project and create a same directory path generally.
Use $_SERVER['DOCUMENT_ROOT'] and then path to your file.
Use PHP's class autoloading, the catch being you then need to wrap up your config information in a class, but it works.
require_once("../../config/db.php");
Include this file into "conf.php" file
Set the include_path directive as either root/ or root/config/ either by editing your php.ini file or by calling set_include_path on every page. This way, you can just include/require "db.php" on each page.

when placeing a config file outside the public root. how do i call it on every page without defining the path

this is how my file structure is. [..] are folders
[public]
[libs]
[crontab]
[logs]
[sessions]
[tmp]
config.php
the domain is set to the public folder as its root. everything else including the config.php file is outside the root. the config contains db connection information and other necessary settings.
every page in the public folder i have to put the entire path so include the config file. problem is when i move everything from my local machine to the public server i have to go through every page and change the path included for the config file. is there a better way? maybe settings a path in php.ini or something like that?
thanks
In that specific case you can use:
include("$_SERVER[DOCUMENT_ROOT]/../config.php");
# Note: special exception array syntax within double quotes.
Which resolves to the public directory first, but moves one level back ../ to reach the config file outside of the docroot.
But you could also use SetEnv in your .htaccess to define another $_SERVER environment variable for the occasion.
A technique that I frequently use to include files relative to a particular location in your webroot structures is to use an include/require like this:
require_once(dirname(__FILE__) . '/path/to/include.php');
This way, codebase migrations don't need to change any hard-links or constants so long as the files remain relative to each other in the same way.
You could just use constant for your path and change it once, and define that constant on every path - included file
You can also take a look at include path
http://php.net/manual/en/function.set-include-path.php
See set_include_path() and the docs for the include_path ini setting.

Categories