PHP fwrite();创建额外的字符
我试图创建一个PHP脚本,通过PHP fwrite();创建额外的字符,php,mysql,Php,Mysql,我试图创建一个PHP脚本,通过$\u POST检索变量,并用所述变量修改另一个PHP文件。但是,每当运行脚本时,将复制要修改的文件的最后12个字符,并将其连接到文件末尾 我试图修改的文件名为database_info.php,如下所示 <?php /** * These are the database login details */ define("HOST", "{HOST}"); // The host you want to connect to. define(
$\u POST
检索变量,并用所述变量修改另一个PHP文件。但是,每当运行脚本时,将复制要修改的文件的最后12个字符,并将其连接到文件末尾
我试图修改的文件名为database_info.php,如下所示
<?php
/**
* These are the database login details
*/
define("HOST", "{HOST}"); // The host you want to connect to.
define("USER", "{USER}"); // The database username.
define("PASSWORD", "{PASSWORD}"); // The database password.
define("DATABASE", "{DATABASE}"); // The database name.
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
数据库_info.php更改为
<?php
/**
* These are the database login details
*/
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "root"); // The database username.
define("PASSWORD", "root"); // The database password.
define("DATABASE", "CMS"); // The database name.
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);| E_STRICT);
您的问题在语句fopen()
如果您阅读了fopen
的文档,则使用参数c
打开文件,它不会截断现有文件,而只是将文件指针放在文件的开头。现在,由于您的POST
值比{..}
值短12个字符,因此它不会“覆盖”最后12个字符,而是用新字符串替换所有内容
简单的解决方法是在fopen
中始终使用w
。所以
$fh = fopen("database_info.php", 'c');
$file = file_get_contents("database_info.php");
将成为
$file = file_get_contents("database_info.php");
$fh = fopen("database_info.php", 'w');
如果在打开文件进行写入后使用文件获取内容
,您将获取空文件进行读取为什么不使用文件获取内容()
/文件放置内容()
?另外,您的问题是错误报告部分(\E\u-STRICT
)正在添加到字符串中吗?哦,我忘了提到在“file\u get\u Content”语句之后应该使用fopen,只需交换它们。就这样做了。非常感谢你。
$fh = fopen("database_info.php", 'c');
$file = file_get_contents("database_info.php");
$file = file_get_contents("database_info.php");
$fh = fopen("database_info.php", 'w');