📲 Download Our Apps

EMI Calculator

Download EMI Calculator

SOA Games

Download SOA Games

SOA Technology App

Download SOA Technology

BMI Checker

Download BMI Checker

Task Jira

Download Task Jira

Laughing Adda

Download Laughing Adda

📅 हिंदी कैलेंडर ऐप डाउनलोड करें

Download Shubhcalendar App

how to write concurrent data in json file in php

<?php
$file = 'data.json';
$fp = fopen($file, 'c+'); // Open for reading and writing, create if not exists

if (flock($fp, LOCK_EX)) { // Acquire an exclusive lock
    $current_data = [];
    if (filesize($file) > 0) {
        $json_content = fread($fp, filesize($file));
        $current_data = json_decode($json_content, true);
        if ($current_data === null) {
            // Handle JSON decoding error
            error_log("Error decoding JSON from $file. Resetting data.");
            $current_data = [];
        }
    }

    // Simulate adding new data
    $new_entry = [
        'id' => uniqid(),
        'message' => 'Hello from process ' . getmypid(),
        'timestamp' => date('Y-m-d H:i:s')
    ];

    $current_data[] = $new_entry;

    ftruncate($fp, 0); // Truncate the file
    rewind($fp);      // Rewind file pointer

    $json_output = json_encode($current_data, JSON_PRETTY_PRINT);
    fwrite($fp, $json_output);

    flock($fp, LOCK_UN); // Release the lock
    fclose($fp);
    echo "Data written successfully by process " . getmypid() . "\n";
} else {
    echo "Could not acquire lock for $file by process " . getmypid() . "\n";
}
?>
 $file = 'activitydata.json';
    $fp = fopen($file, 'c+');   

if (flock($fp, LOCK_EX)) {
    $arrayData = [];
     if (filesize($file) > 0) {

        $currentData =  fread($fp, filesize($file));
        $arrayData = json_decode($currentData, true);  // true for associative array
            if ($arrayData === null) {
            // Handle JSON decoding error
            error_log("Error decoding JSON from $file. Resetting data.");
            $arrayData = [];
        }
     }
       $arrayData[] = $success;
       ftruncate($fp, 0); // Truncate the file
       rewind($fp);      // Rewind file pointer
         $jsonData = json_encode($arrayData, JSON_PRETTY_PRINT); // JSON_PRETTY_PRINT for readability
         fwrite($fp, $jsonData);
         flock($fp, LOCK_UN); // Release the lock
         fclose($fp);
}



Leave a Reply

AI Spiritual Tools & Interactive Experiences

Explore powerful AI-driven tools for daily guidance, spirituality, fun quizzes, and self-discovery.

Today’s Quote

Get inspiring daily quotes powered by AI to motivate and guide your day.

Explore Now

AI Tarot Card Reader

Reveal insights about your future, love, and career with AI tarot readings.

Read Tarot

Love Match Calculator

Check compatibility and love predictions using AI-based analysis.

Check Match

Fortune Cookie

Open an AI fortune cookie and receive wisdom, luck, and fun messages.

Open Cookie

Quiz Categories

Engage with knowledge-based and fun quizzes across multiple categories.

Start Quiz

Panchang Calendar

View daily Panchang, auspicious timings, tithi, nakshatra, and festivals.

View Panchang

Online Numerology

Discover your destiny number, life path, and numerology predictions.

Calculate Now

Spiritual Feeds

Stay connected with spiritual thoughts, mantras, and divine content.

View Feeds

Quiz Hub

Attempt trending quizzes on GK, spirituality, festivals, and more.

Explore Quizzes