Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:46:10+00:00 2026-06-09T01:46:10+00:00

I am trying to write data to a php file everytime the page is

  • 0

I am trying to write data to a php file everytime the page is refreshed. The code below successfully writes to a file, however, the file contents remain the same after the first rewrite, even though the the data contained in the array that is used to write to the file is different every time the page is refreshed.

My question is: how can I rewrite or replace the contents of .php file that already exists?

I tried unlink('../html/freesample2.php'); to solve this problem, by deleting the ‘written’ file and then recreating it, however it did not help.

I appreciate any advice.

//Create variable for file I want to write to
//second parameter 'a' stands for APPEND
$f = fopen('../html/freesample2.php', 'a') or die('fopen failed');

$php_script= '<?php $free_sample_array_new = Array(); $free_sample_array_new[] = '.$free_sample_array[0].'; $free_sample_array_new[] = '.$free_sample_array[1].'; ?>';

fwrite($f, $php_script);

fclose($f);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:46:13+00:00Added an answer on June 9, 2026 at 1:46 am

    If you want to open a file for writing, you can use 'w' as the access mode for fopen(). That will create the file if it’s not there yet, or truncate it to be empty and you can write into it as if it’s new.

    Alternatively you can use file_put_contents to make the operation a oneliner.

    side note: If you are generating php code with literal values, take a look into var_export function, that could simplify it vastly (with the second, optional parameter).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write data to a simple txt file in PHP and open
I'm trying to write a php function that gets all the basic food data.
I am trying to write data stored in a binary file using turbo C++.
I am trying to write my spatial data from a table to a file.
I want to write data into the file in binary form. I was trying
I am trying to send data from an Android app to a PHP file
I'm trying to write a simple php script to take in data from stdin
I am trying to write a php script that take a text file break
I'm trying to write a sizable chunk of data to a file that is
I'm trying to load a javascript file into a PHP page and then i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.