Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Create or Replace File Content in Laravel

How to Create or Replace File Content in Laravel ?

Aman Jain, May 29, 2022November 9, 2023

This article will guide you to Check File exist and Create or Replace File Content in Laravel. To perform the file system based operations like Check file exist and create or replace file in laravel. we use File class or Storage class in laravel. Laravel provides inbuilt library to access the file system and we can do multiple robust operations using the libraries. In this tutorial i will show you to make a file with content in laravel.

Sometimes we want to store raw information in text or json file so for this purpose we need to create the file in our application. File can be open in different modes like append, read only or read and write.

In this article I will use Illuminate\Filesystem\Filesystem class and facade File::put() , File::replace(), File::prepend() and FIle::append() method to create and replace a new file . put Method accepts two parameters as below

1. File::put() : To create new file and add the content

File::put($path, $contents, $lock)

Example Usage:

File::put(public("logs/user.text"),"Simple content");

2. File::replace() : To Replace the content of existing file

File::replace($path, $contents)

Example Usage:

File::replace(public("logs/user.text"),"Simple content");

Here I called put with 2 parameters first parameter tells to the name of file and second parameter to add the contents to the file

Let’s understand it with example step by step

Step 1: Create a fresh laravel project

Open a terminal window and type below command to create a new project

composer create-project --prefer-dist laravel/laravel blog

You can also read this to start with new project

Step 2 : Create controller

Let’s create a controller and add method checkAndCreateFile

php artisan make:controller FileController

and add the below code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
class FileController extends Controller
{
    public function checkAndCreateFile(Request $request){
        
        
            $storageDestinationPath=public_path("users.log");
         
            if (!File::exists( $storageDestinationPath)) {
                File::put($storageDestinationPath,"This is simple content");
            }
            else{
                File::replace($storageDestinationPath,"This is simple content replaced");
            }
            return response("File created/replaced successfully");
         
  
    }
}

In above code First of all we are checking the file is exist or not then if not available then we are creating the file at the location and if already exist then we are replacing the content of file

Step 3: Create two routes in routes/web.php

Create a route to create the directory

routes/web.php

<?php

use App\Http\Controllers\checkAndCreateFile;
use Illuminate\Support\Facades\Route;

Route::get("/create-file",[FileController::class,"checkAndCreateFile"]);

Related

Php Laravel Laravel 9 createexistfilelaravelreplace

Post navigation

Previous post
Next post

Related Posts

how to add multiple select in laravel eloquent ?

December 29, 2021February 22, 2024

Laravel eloquent builder has rich features to modify the query. In some cases we wanted to add multiple select in laravel and modify the select statement on basis of few conditions and wanted to add some select statement on condition. so in this article we will learn to add multiple…

Read More
Laravel validate checkbox in laravel

How to validate checkbox in laravel ?

January 26, 2024March 16, 2024

Certainly! Below is a step-by-step guide on how to validate checkbox in Laravel application. Let’s assume you are working on a form that includes checkbox and you want to make sure that at least one checkbox is selected before the form is submitted. We will take a simple example as…

Read More

Create Custom Class or Custom Library in Laravel 10

March 14, 2023March 16, 2024

In Laravel, you can create custom classes or custom library in laravel that can be used throughout your application. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. Thus I this article i will show you to create a class and use…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version