1

code: public/index.php

<?php // $path = base_path() . '/.env'; if(file_exists('/../.env')) { echo "yes"; } else { echo "No"; } 

In this code, I have .env file in my root directory but when I try to know my .env file exists or not then it shows No. I had also tried with base_path() after this it throws 500 error. I don't know why? So, How can I fix this issue? Please help me.

Thank You

4
  • you sure the path is correct? Commented Oct 31, 2019 at 7:11
  • yes, I am sure @Areg Commented Oct 31, 2019 at 7:12
  • 1
    it's better to figure out why base_path() is not working. In your code definitely you get wrong result because you start path with slash '/'. You can try change it to './../.env' (add dot at the beginning) or play more with path value, but this is not the best idea to reuse this code, because your path is relative to working directory Commented Oct 31, 2019 at 7:14
  • 1
    You need to use absolute path. Not relative path. Try $_SERVER['DOCUMENT_ROOT'] . /folder_name/.env. Commented Oct 31, 2019 at 7:17

2 Answers 2

2

Sometimes some IDE dont show the .env file. Its hidden in project .

In laravel, search .env.example file .

you can do following step in cmd or terminal (in your project folder)

cp .env.example .env

php artisan key:generate

Sign up to request clarification or add additional context in comments.

Comments

1

Put below in your routes:

Route::get('/test', function() { $path = __DIR__ . '/../.env'; if(file_exists($path)) { return '<h1>Yes</h1>'; } return '<h1>No</h1>'; }); 

and try visiting the link like example.com/test.

Note: For security reasons, Laravel's .htaccess file should not allow you to run your script without route.


But if you are sure it is removed you could create it like the .env file template below:

APP_NAME=Laravel APP_ENV=local APP_KEY= APP_DEBUG=true APP_URL=http://localhost LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file QUEUE_CONNECTION=sync SESSION_DRIVER=file SESSION_LIFETIME=120 REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET= PUSHER_APP_ID= PUSHER_APP_KEY= PUSHER_APP_SECRET= PUSHER_APP_CLUSTER=mt1 MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}" MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}" 

then run:

php artisan key:generate php artisan config:cache 

1 Comment

@KennedyOwusu note that APP_DEBUG=true is a backdoor, which allows people to SSH into your server (so ensure to set that to false, once published on Ubuntu server).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.