$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Using fwrite() and "a" appends multiple times instead of once|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Using fwrite() and "a" appends multiple times instead of once

matteradmin8PV0评论

I'm doing some testing as I'm new to PHP and Wordpress.

On refresh the following code runs functions.php

<?php

$content = "some text here\n";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/zerif-lite-child/myText.txt","a");
fwrite($fp,$content);
fclose($fp);
?>

Which writes to the .txt file multiple times! If I use "wb" it will write to the file only once, but I want this code to append the file, not overwrite it everytime.

I've tried using flock() but that produces the same result.

Why is it writing to this file multiple times when I append it?

I'm doing some testing as I'm new to PHP and Wordpress.

On refresh the following code runs functions.php

<?php

$content = "some text here\n";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/zerif-lite-child/myText.txt","a");
fwrite($fp,$content);
fclose($fp);
?>

Which writes to the .txt file multiple times! If I use "wb" it will write to the file only once, but I want this code to append the file, not overwrite it everytime.

I've tried using flock() but that produces the same result.

Why is it writing to this file multiple times when I append it?

Share Improve this question asked Sep 28, 2015 at 11:04 user81086user81086 112 bronze badges 5
  • Trying to clarify: How many lines do you get if you refresh once? – Aravona Commented Sep 28, 2015 at 11:09
  • 1 Have you tried using the WP File system API? What're you trying to do? Are you trying to create an error/debug log? – Tom J Nowell Commented Sep 28, 2015 at 11:52
  • Or file_put_contents() with FILE_APPEND flag? Edit: Sorry, misunderstood, appending isn't the problem.. Put you code into a function and hook it to e.g. wp_loaded. So some thing like: add_action( 'wp_loaded', 'write_to_file_function' ); function write_to_file_function() { //your code } – Nicolai Grossherr Commented Sep 28, 2015 at 12:00
  • Are you using a parent-child-theme-combination? And if so, do you have the code in both functions.php's? Because they will both get loaded, which would explain the multiple times execution. – Nicolai Grossherr Commented Sep 28, 2015 at 12:17
  • @ialocin is on the right track. Something about your implementation is causing the code the run multiple times. Using wb is probably doing the same thing, you just can't tell because the file is overwritten every time. – s_ha_dum Commented Sep 28, 2015 at 14:37
Add a comment  | 

1 Answer 1

Reset to default 1

You could try putting your code in a WordPress Action or Filter call:

add_action( 'init', 'my_file_function' );

function my_file_function() {
    $content = "some text here\n";
    $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/zerif-lite-child/myText.txt","a");
    fwrite($fp,$content);
    fclose($fp);
}

The flag ('a' vs 'wb') doesn't have anything to do with your problem, it just happens to showcase it. For whatever reason, your functinos.php seems to be running more than once. You could start with a fresh copy of the default theme and try your code again, but you should also get in the habit of writing your code inside of an action call.

This helps ensure that the code you want to run is running at the appropriate time. It also allows you to hook in to various places in WordPress and generally makes coding for it much more powerful.

Post a comment

comment list (0)

  1. No comments so far