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

How to Prepend or Append File Content in Laravel ?

Aman Jain, May 29, 2022May 29, 2022

In our last article I showed you to create file and this article will cover to Prepend or Append File Content in Laravel application. To perform the file system based operations like create, replace, prepend or append File Content 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 append or prepend raw information in text or json file so for this purpose we need to 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::prepend() and FIle::append() method to create and replace a new file . put Method accepts two parameters as below

1. File::prepend() : To append the content in the file at last.

File::prepend($path, $contents, $lock)
File::prepend(public("logs/user.text"),"Simple content");

Example Usage:

2. File::append() : To append the content at the beginning of the existing file.

File::append($path, $contents)

Example Usage:

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

Here I called append or prepend 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 checkAndAppendFile

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 checkAndAppendFile(Request $request){
        
        
            $storageDestinationPath=public_path("users.log");
             File::append($storageDestinationPath,"This is simple content appended");
            
            return response("File changed successfully");
         
  
    }
}

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("/append-file",[FileController::class,"checkAndAppendFile"]);

Related

Php Laravel Laravel 9 existfilelaravelprepend

Post navigation

Previous post
Next post

Related Posts

Devops install php 8 in ubuntu

How to install PHP 8.0 in Ubuntu 20.04 LTS ?

September 18, 2021November 5, 2023

In this tutorial we will learn to install PHP 8.0 on Ubuntu 20.04 LTS using PPA. PHP 8.0 is the latest version of php and it have many upgrades. Installation process is same as How to install PHP on Ubuntu? or How to Install multiple/different version of PHP? Step 1…

Read More

Enabling and Disabling Debug Mode in Laravel: A Step-by-Step Guide

July 6, 2023March 16, 2024

Debug mode is a useful feature in Laravel that provides detailed error messages and stack traces during development. However, it’s essential to disable debug mode in production to ensure the security of your application. In this blog post, we will walk you through the steps to enable and disable debug…

Read More
Php How to Use Broadcast to Send Web Socket Event in Laravel

How to Use Broadcast to Send Web Socket Event in Laravel ?

June 23, 2022June 26, 2022

Broadcasting is used to send real time message to client. In this article we will learn to use broadcast to send web socket event in laravel. Web sockets are used to communicate with client from server for real time communication. Broadcasting very useful to send update to client when any…

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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version