Hire the author: Ngei S

laravel unable to open file for reading

Click here to view the code on GitHub.

Introduction

“Laravel Unable to open file for reading” is a common laravel error. Laravel is one of the best model view controller (MVC) frameworks today. As a developer, I can build any web-based program with it including: CRMs like Bitrix24, Task Managers like Trello, and many more applications. As a developer, you are also able to integrate with communication channels like emails and SMS. In today’s blog, I will be writing about how to solve an error that mostly occurs while sending an attachment file using Laravel.

My goal is to show how I solved it. To be able to accomplish this project you need a decent understanding of PHP and object-oriented programming. I would recommend also for the operating system you use Ubuntu Linux. You also need a basic understanding of the Laravel framework or any MVC frameworks.

Glossary

  • Composer – is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
  • Git – Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. If you are using Windows git will help you like when you right-click you can open the terminal on any directory.
  • Laravel – Laravel is a web application framework with an expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
  • PHP – PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible, and pragmatic, PHP powers everything from your blog to the most popular websites in the world.
  • Xampp – XAMPP is a completely free, easy to install Apache distribution containing MariaDB, PHP, and Perl. The XAMPP open source package has been set up to be incredibly easy to install and to use.
  • Browser – a computer program with a graphical user interface for displaying HTML files, used to navigate the World Wide Web.

Note: For this tutorial, we will be using Xampp Server. You can use any other web server you are familiar with.

Setup

Let’s cover how to solve “Laravel unable to Open File for Reading”. We need to install Xampp, Once you have installed xampp you need to install the composer. Both provide clear documentation on how to install them depending on your operating system you might be using. To confirm either if xampp has been installed in your local machine for;

  • Windows – Start the xampp control panel and start the Apache server
  • Linux – Run the following Commands
    • sudo /opt/lampp/lampp start. The following screenshot is the results that you are supposed to see after updating
laravel unable to open file for reading

To confirm if you have successfully installed composer, just run command composer on you terminal. The output is supposed to be as follows

composer
laravel unable to open file for reading

Now we have successfully installed both xampp and composer we are now ready to get started.

Cause of the error.

The error “Unable to open a file for reading” is caused if you try to access the file’s exact path in ways other than using the set functions by laravel. The following image shows the error.

For instance, in send mail function. Let’s say I store the image first before I send it. This will mean it’s already in the public directory. If I access it just like in the following code, then this error will occur.

Step 1: Installing Laravel

To install laravel just run the following command. projectName is the name of the project/folder.

composer create-project laravel/laravel projectName

After laravel has been installed, you are now able to access your project. One advantage of using laravel is that laravel has its own server. To be able to access this server, make sure you are inside the current working directory of your project. Then type the following command. You can also copy your project folder inside htdocs and access the folder just as a normal PHP project.

php artisan serve

If you have copied the project at htdocs, you can access the project like any other php project i.e http://127.0.0.1/projectName/public. This will be able to access the project.

Step 2: Creating a Controller

To be able to solve “laravel unable to Open File for Reading” we need to create a controller. To create a controller in laravel run the following command,

php artisan make:controller ControllerName

Step 3: Creating a Function to solve laravel unable to open file for reading

Once you have created your controller, you can view it at app/Http/controllers. Open it and create a function for sending an email. For instance

public function sendEmail(Request $request){
}

Once you are done creating the function, add the following code to that function you have already created.

Step 4: Adding Sweet Alerts in laravel files

After a success or any failure they pop up depending on the state. You can install them with the following command inside your project directory

composer require realrashid/sweet-alert

And run the following command

php artisan vendor:publish

Select the position where sweet-alert is and press enter. This will be able to publish the package and ready for usage.

Step 5: Adding Routes in Laravel

Once done creating a function, we are now ready to create a route to be able to access that function. Navigate to routes and select web.php. Since we are submitting a web form, this means that that method is supposed to be post. The following is a route for that method

Route::post('attachment/add','MailController@sendEmail');

Step 6: Modifying .env File

.env file is also known as environment file. If you are using a google email you need to change setting in security to allows a less secure application to access your email. This file, you will modify it in such a way that it will have have a sender’s email just as follows:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=//imput your email
MAIL_PASSWORD=//imput your password for the mail
MAIL_FROM_ADDRESS=//Input email
MAIL_FROM_NAME=//senders name
MAIL_ENCRYPTION=tls

Step 7: Creating Views

Create Email Views

After creating a route, let’s create a view. View contains the HTML content served by your application. In our case I used already created view that comes with laravel, the welcome view. you can create a new one. Navigate to resources/views the inside the views directory create a new file with the following format.

for example: email.blade.php

Use the following HTML code for your view

We also need to create another view, in our method in this line

        $sendMail = \Mail::send('mails',$emailData,function ($message) use($data,$url)

This view will be just a simple HTML document that will mostly carry the message of the email send. The content of the view will be as follows

Layouts

Once you are done creating the view, you can customize it the way you need your mails to be like. After you are done, you need to pass the following variable inside the view. For layouts, laravel comes with an already packed view that contains all CSS for the welcome page. Found on resources/views/layouts. Its content is as follows.

Step 8: Accessing The Project

We are now ready to serve our project. You have two methods to do so

  • Use this command
php artisan serve
  • You can copy your project to htdocs and access it just like any other php project
localhost/projectName/public

Learning Tools

There are a lot of learning equipment which include

  1. Laravel Docs – The no 1 website stuck you supposed to reference first before searching elsewhere.
  2. PHP – For any PHP related problem always refer here
  3. Stack overflow – This is a community of developers, you can find some answers and some may never work for you but its a good place to check out. You may find this question “How to solve Laravel Unable to Open File for Reading.” but most of them won’t help you.
  4. Google – Good place to refer too. You may find this question “How to solve Laravel Unable to Open File for Reading.” but most of them won’t help you.

Learning strategy

To be able to complete this blog project, I used the above tools. I recommend the use of google search to the maximum level. If by any chance you’re experiencing errors it is okay to ask for help from colleagues or developers communities around the globe. I generally ask colleagues on LD Talent‘s slack org. It really helps.

Reflective Analysis

Sending an email using laravel can sometimes be challenging if you’re a beginner. Once you completely understand how to do it, things become easier, and you become less of a beginner. For more information, you can read on it on Laravel’s docs.

Conclusion

This is a simple send attachment project using laravel. You can use the same code to send an email without an attachment.

Hire the author: Ngei S