admin管理员组文章数量:1130349
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 |1 Answer
Reset to default 1You 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.
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()withFILE_APPENDflag? 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
wbis 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
1 Answer
Reset to default 1You 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.
本文标签: phpUsing fwrite() and quotaquot appends multiple times instead of once
版权声明:本文标题:php - Using fwrite() and "a" appends multiple times instead of once 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749165013a2326029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


file_put_contents()withFILE_APPENDflag? 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:00functions.php's? Because they will both get loaded, which would explain the multiple times execution. – Nicolai Grossherr Commented Sep 28, 2015 at 12:17wbis 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