Listado de la etiqueta: Tutorial

What Is File Validation in Laravel?

Laravel validation is a way to perform a validation role. We can check the file type, file size, etc. File validation is typically used to avoid unwanted file uploads in a server or application.

Today, we will learn about file upload and storing in Laravel 9.

Project requirements are provided below:

  • MySQL 8.0+
  • MariaDB 10.2+
  • PHP 8.1

Here is an example of defining the FileUpload:

  1. Process 1. Create a FileUpload Project
  2. Process 2. FileUpload Project Database Connection
  3. Process 3. Create a Model and Controller in the FileUpload Project
  4. Process 4. Run and Test the FileUpload Project

Process 1. Create a FileUpload Project

Step 1 is to run this command line to install Laravel 9 on our server or pc. For that, we need to open the Windows SSH terminal in the folder.

Composer createproject Laravel/Laravel FileUpload

Process 2. FileUpload Project Database Connection

Open the .env file on the FileUpload project and add a new database name, username, and password.

Here is the following code:

DB_DATABASE= FileUpload Database name
DB_USERNAME= FileUpload Database user name
DB_PASSWORD= FileUpload Database password

Process 3. Create a Model and Controller in the FileUpload Project

Merienda the database is connected, we must create a model and controller for the FileUpload Project.

For that, we need to run the following command:

php artisan make:model FileUpload mc

Now, go to “databasemigrations2022_07_11_074014_create_file_uploads_table.php” file. This name could be different on your side. Now, we need to put these two lines in the file:


Its looks like this

return new class extends Migration
    public function up()
        Schema::create(‘file_uploads’, function (Blueprint $table) {

    public function down()

We modify our FileUpload model in “appModelsFileUpload.php”. We add fillable property there. The code is shown below:


namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class FileUpload extends Model
    use HasFactory;
    protected $fillable = [

Now, we need to migrate the table to the database to run this command:

Merienda migration is completed, we need to create two functions. One function is for going to the upload page, and another is to upload a file in the directory and store it in the database.

Go to the “appHttpControllersFileUploadController.php” file:

Now, add this code to the controller:

 public function fileview(Request $request)
        return view(‘file-upload’);

And another function is shown below:

 public function uploadTheFile(Request $request)

            $rules = array(
                ‘attachment’ => ‘mimes:jpeg,png,jpg,gif,svg,pdf|max:1000’,
            $messages = array(
                ‘attachment’ => ‘ Image need Less then 1Mb.’,

            $validator = Validator::make($request->all(), $rules, $messages);
            if ($validator->fails()) {
                return redirect()->route(‘fileviews’)->with(‘success’, ‘Successfully Added’);
                return redirect()->route(‘fileviews’)->withErrors($validator);
                $imageName = time() . ‘.’ . $request->attachment->extension();
                //remove image before upload

               $path = $request->attachment->move(public_path(‘storage/attachment/’), $imageName);
               $departmentname[‘file_name’] = $imageName;
                $departmentname[‘file_path_location’] = $path;
                return redirect()->route(‘fileviews’)->with(‘error’, ‘No file added’);


In the uploadTheFile function, we used the Validator.

We created this function to upload the view. Thus, we need to create the view in the Laravel view located in “resourcesviewsfile-upload.blade.php”.

Let’s build the view for uploading the file.

Copy the code and paste it into the view.

<!DOCTYPE html>
<html lang=«en»>

    <meta charset=«utf-8» />
    <meta name=«csrf-token» content=«{{ csrf_token() }}»>
    <meta name=«viewport» content=«width=device-width, initial-scale=1.0»>
    <!– CSS only –>
    <link href=«[email protected]/dist/css/bootstrap.min.css» rel=«stylesheet»
        integrity=«sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBl6DOitfPri4tjfHxaWutUpFmBp4vmVor» crossorigin=«anonymous»>
    <!– JavaScript Bundle with Popper –>
    <script src=«[email protected]/dist/js/bootstrap.bundle.min.js»
        integrity=«sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2» crossorigin=«anonymous»>

    <div clas=«container»>
        <div class=«col-lg-12»>

            <div class=«card-box»>
                    @if ($errors->any())
                    @foreach ($errors->all() as $error)


        <div class=«col-lg-12»>
            <div class=«card-box»>
                <form action=«{{ route(‘uploadTheFile’) }}» method=«POST» enctype=«multipart/form-data»>
                    <div class=«form-group»>
                        <label for=«name»>Upload FIle</label>
                        <input type=«file» class=«form-control» name=«attachment» required>

                    <div class=«text-right»>
                        <button type=«submit» class=«btn btn-success waves-effect waves-light»>Save</button>



Now, we need to create two routes for uploading files in our project:

Route::get(‘/file-view’, [FileUploadController::class, ‘fileview’])->name(‘fileviews’);
Route::post(‘/file-upload’, [FileUploadController::class, ‘uploadTheFile’])->name(‘uploadTheFile’);

Merienda the route is created, go to the “/file-view” and it should look like the following image in the browser:

Process 4. Run and Test the FileUpload Project

Now, we need to run the command and check file upload if it is working or not:

We need to check whether the code is working or not through the route for checking.

Select a file and try to upload it. In my case, it worked fine.

Merienda a file is uploaded, it can be found in the following:

Image is here

Also, it is inserted into the Database.


In this article, we created this Laravel FileUpload project with Laravel 9. Creating a data table in Laravel using FileUpload is very useful to call the file again. We hope this FileUpload project example will help you to understand the file Upload in Laravel 9.

Source link

One way to enhance your Linux system’s security is by adding an extra security layer using SELinux. With Security-Enhanced Linux (SELinux), the applications on your Linux systems get isolated from each other, protecting your host system. By default, Ubuntu uses the AppArmor, a Mandatory Access Control system which enhances the security, but you can use the SELinux to achieve the same.

SELinux is beneficial, and in case of a security breach on your system, it prevents the spread of the breach to protect your system. Moreover, the tool protects the web servers depending on the mode you set for the SELinux. This guide offers a hands-on tutorial on how to disable the AppArmor, install the SELinux, enable the different modes, and disable SELinux.

Getting Started with SELinux

Note that before you proceed with SELinux, there is a risk in using it, especially since it can render your system unusable. So, only use it if you must and in such applicable case. Besides, it’s always safer to disable the AppArmor before installing the SELinux.

To disable the AppArmor, run the following command:


$ sudo systemctl stop apparmor

Merienda the AppArmor stops, restart your system.

How to Install SELinux on Ubuntu

Merienda you disable or remove the AppArmor, open your terminal and run the following command to install SELinux.


$ sudo apt update

$ sudo apt install policycoreutils selinux-utils selinux-basics

Merienda the installation is successful, you need to activate the tool. You can do that using the following command:

Enabling SELinux Modes on Ubuntu

There are three different modes that you can use with SELinux. The first is disable, which does the same as its name. It disables using the SELinux service. When SELinux is activated, you can set it to permissive or Enforcing modes. In the permissive mode, only the monitoring of the interaction is done. However, if you want to filter and celador the interaction, use the enforcing mode.

Let’s start by setting the enforcing mode. Use the following command:


$ sudo selinux-config-enforcing

Alternatively, you can use the setenforce command to set the enforcing mode. The command for this is as follows:

Merienda you set the mode, you need to reboot your system for it to take effect.

Note that the relabelling process starts during the restart. The system reboots normally merienda it is complete. During relabelling, you should note a warning message like in the following image:

After a successful reboot, you can run the following command to check the SELinux status. It should be set to enforcing.

The enforcing mode is the default set by SELinux. In this state, most if not all the requests get blocked. The solution is to select the permissive mode, which logs all the violated rules. You can check the log file for details.

To set the permissive mode, use the following command:

Go ahead and check the mode using the setstatus command or use the getenforce command:


$ setstatus


$ getenforce

With getenforce, you will only see the name of the current mode, but the setstatus shows more details about the currently set mode.

Note that you must restart the system to switch between the two modes. Besides, you can view the set modes from the /etc/sysconfig/selinux file.

As we noted, the permissive mode is more flexible and won’t necessarily block all the requests. Instead, it keeps a log file when the rules get violated. To access the log file, you can use the following command:


$ grep selinux /var/log/audit/audit.log

To set the permissive mode, use the following command:

How to Disable SELinux

We’ve seen how to enable and set the different SELinux modes. But how about disabling it? The best option is to disable it from the config files permanently. For this, open the file using an editor like nano. Then, change the mode from enforcing to disabled, as shown in the following command:


$ sudo nano /etc/selinux/config

Merienda opened, look for the SELINUX=enforcing line and change it to SELINUX=disabled.


The AppArmor is the extra security layer in Ubuntu and other Linux systems. However, if you prefer using the SELinux, we’ve covered how you can install, enable, and use its different modes. Before installing the SELinux, make sure you disable the AppArmor and restart the system. Also, proceed with caution when using the SELinux to avoid messing up with your system.

Source link