Using Laravel to upload a file

Uploading a file in any programming is a challenge. So, we focus on uploading a file and some validations to use with file upload using Laravel.

Uploading a file in any programming is a challenge. In this post, we focus on uploading a file and some validations to use with file upload using Laravel.

To upload a file using Laravel, you can follow these steps:

Create a new form in your Laravel view with an input field for the file:

<form method="POST" action="{{ route('file.upload') }}" enctype="multipart/form-data">
    @csrf

    <input type="file" name="file">

    <button type="submit">Upload</button>
</form>

In above code, we have added file.upload route as an action of the form. So, we need to define this route in routes/web.php file. This route should point to the controller method that will handle the file upload.

The following code will define a new route in your routes/web.php file:

Route::post('/file/upload', [App\Http\Controllers\FileController::class, 'upload'])->name('file.upload');

Above code has defined a route, which points to the upload method of the FileController. So, create a new controller FileController and method upload inside that to handle the file upload as follows:

class FileController
{
    public function upload(Request $request)
    {
        // Validate the uploaded file
        $request->validate([
            'file' => 'required|file|max:1024', // limit file size to 1 MB
        ]);

        // Store the uploaded file in the storage/app/public directory
        $path = $request->file('file')->store('public');

        // Generate a URL for the uploaded file
        $url = Storage::url($path);

        // Redirect back with a success message
        return back()->with('success', 'File uploaded successfully: ' . $url);
    }
}

In the upload() method, we first validate that the uploaded file meets our requirements. So, we have added some validations for our file. These validations are as follows:

required:

The field under this validation must be present in the input data and must not empty. A field is “empty” if it meets one of the following criteria:

  • The value is null.
  • The value is an empty string.
  • The value is an empty array or empty Countable object.
  • The value is an uploaded file with no path.

file:

The field under this validation must be a successfully uploaded file.

max:1024:

The field under this validation must be less than or equal to a 1024 bytes. Here, 1024 is value of file size. You can change it according to your requirements.

We then use the store() method on the uploaded file to store it in the storage/app/public directory. This directory is publicly accessible, so we can generate a URL for the file using the url() method on the Storage facade. Finally, we redirect back to the form with a success message that includes the URL of the uploaded file.

You can now test the file upload functionality by navigating to the form and selecting a file to upload. If the file meets the validation requirements, it will be uploaded and a success message will be displayed. You can then access the uploaded file at the generated URL.

Getting started with ReactJS

Setting up a local environment is recommended way to learn ReactJS because the local setup allows you to complete the tutorial using your choice of editor, use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production.

Setting up a local environment is recommended way to learn ReactJS because the local setup allows you to complete the tutorial using your choice of editor, use the latest JavaScript features, provides a nice developer experience, and optimizes your app for production.

Let’s start with creating a new ReactJS application.

Create React App

Creating React App is a comfortable environment for learning and building an application in React.

There are few prerequisites for creating a React app as follows,

  • Node.js >= 14.0.0
  • npm >= 5.6

If above requirements are satisfied, you can run the following command to create a new react application,

npx create-react-app my-react-app

You can change my-react-app to anything you want to name your app.

The create-react-app command will set up everything you need to run a React application. Now you are ready to run your first real React application! Go to your application directory using the following command,

cd my-react-app

Now, run the following command inside your application directory to run your application,

npm start

It will compile your application and open it in the new browser window or tab. If not, you can open your browser and write localhost:3000 in the address bar. Your application will look like screenshot below,

If you check your command window, it will show the following output,

You can see that command window is also providing command to create a production build for your application.

Using React JS in the HTML File

We will learn to use ReactJS directly in the HTML file. There are some prerequisites to adding ReactJS script inside the HTML code.

Now that we know what React JS is, we can start with different usage of ReactJS. If you don’t know what React JS is, read the Getting Started with ReactJS.

In this article, we will learn to use React JS directly in the HTML file. There are some prerequisites to adding React JS script inside the HTML code. We need to include the following three javascript in the head section of the HTML file.

The first two allow us to write ReactJS code in our JavaScripts, and Babel Js will allow to write JSX syntax and ES6 in older browsers.

Follow the below steps to add react JS compnent to the HTML file.

Add the DOM container to mount ReactJS Component

Embedding React JS in HTML differs slightly from what we have done with React Js App. In a standard React Js app, the root element in the index.html is the container that mounts the React Js app. Similarly, we need any element to mount our react JS component in HTML file as below,

<div id="example"></div>

In above code, we have added an element with id example in HTML file.

Add required JS files to run ReactJS

We want to run a React app in an HTML file, where no node_modules to provide React Js dependencies. Therefore, we must add React dependencies by including React script tags in the HTML document as below,

<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

In above code, we have included the required React JS scripts, which are required to execute the React JS code.

React Js code returns JSX. So, we also need Babel JS to transform that JSX as below,

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

Write ReactJS code in HTML

Now, our HTML file is ready for react JS code. But, we have to add our react JS code in text/babel script tag as babel can transpile our react JS code as below,

<script type="text/babel">
  function Hello() {
    return <h1>Hello World!</h1>;
  }
  ReactDOM.render(<Hello />, document.getElementById('example'))
</script>

Example

The complete code of using React JS in the HTML file is as follows,

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  </head>
  <body>
    <div id="example"></div>
    <script type="text/babel">
      function Hello() {
        return <h1>Hello World!</h1>;
      }
      ReactDOM.render(<Hello />, document.getElementById('example'))
    </script>
  </body>
</html>

This way, we can use ReactJS for testing purposes. But for production use, we have to set up a React environment.

ReactJS Introduction

ReactJS is one of the most popular JavaScript front-end libraries for building user interfaces with a strong foundation. It has a robust and rapidly growing developer community and a declarative, efficient, and flexible library for building reusable UI components. It is an open-source, component-based front-end library responsible only for the application view layer. 

ReactJS is one of the most popular JavaScript front-end libraries for building user interfaces with a strong foundation. It has a robust and rapidly growing developer community and a declarative, efficient, and flexible library for building reusable UI components. It is an open-source, component-based front-end library responsible only for the application view layer. 

Initially, it was developed by Jordan Walke, a software engineer at Facebook and maintained by Facebook. Later, it was used in other Facebook products like WhatsApp & Instagram. Facebook implemented ReactJS in its newsfeed section in 2011, but it was released to the public in May 2013.

The main objective of ReactJS is to develop interactive User Interfaces (UI) with the help of virtual DOM (JavaScript object) to improve application performance because the JavaScript virtual DOM is faster than the regular DOM because, it only changes individual DOM elements instead of reloading complete DOM every time. It uses component and data patterns that improve readability and helps to maintain larger apps.

The components are the heart of all React applications. These component are used to make a React application. Each component is responsible for outputting a small, reusable piece of HTML code and can be nested with other components to allow complicated applications.

Solved – error while loading shared libraries: libpangox-1.0.so.0: Anydesk on Ubuntu 22.04 LTS

After successfully upgrading from Ubuntu 20.04 LTS to 22.04 LTS, most of the applications are working perfectly. But, some of the applications behave unusually. Such as, I tried to run the Anydesk application, but it doesn’t launch/start. when I checked the status service, the AnyDesk service was failed and the reason for failing is mentioned in the below error message,

anydesk: error while loading shared libraries: libpangox-1.0.so.0: cannot open shared object file: No such file or directory

libpangox-1.0.so.0 is a library used for text layout and rendering the text. Most of the work on Pango-1.0 was done using the GTK+ widget toolkit as a test platform.

So, I ran the command to install it using the apt as follows,

apt install libpangox-1.0-0

But, it gives me the following error,

Package libpangox-1.0-0 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

So, from that we can understand that, libpangox-1.0.so.0 can’t be installed from apt or apt-get. We need to install it manually.

Use the following steps to install libpangox-1.0.so.0 manually,

Step 1. Download the libpangox-1.0 package

wget http://ftp.us.debian.org/debian/pool/main/p/pangox-compat/libpangox-1.0-0_0.0.2-5.1_amd64.deb

Step2: Install the package using apt

sudo apt install ./libpangox-1.0-0_0.0.2-5.1_amd64.deb

Step3: Restart the AnyDesk service

sudo service anydesk restart

After these steps, if you check the AnyDesk service status, it will show active (running).