我正在尝试创建一个脚本,该脚本将在创建/更新项目时发布存储在文本文件中的通知。 如果项目已创建,则需要创建文件;如果项目正在更新,则需要更新现有的 txt 文件。
到目前为止,我已经可以创建 txt 文件并更新它。但是它只更新一次,如果我尝试再次更新文件,它就不会执行此操作。
代码如下:
function addPost($userID, $content, $date, $featured, $projectID){
if($projectID != 0){
$addClass = 'normal';
$sql1 = "SELECT * FROM `blog` WHERE `projectID` = ".$projectID;
$query1 = mysql_query($sql1);
$num_rows1 = mysql_num_rows($query1);
if($num_rows1 != 0){
$addClass = 'internalUpdate';
}
}else{
$addClass = 'normal';
}
switch($addClass){
case normal;
//SQL to get next ID
$nameQuery = mysql_query("SELECT MAX(ID) FROM `blog`");
$nameRow = mysql_fetch_array($nameQuery);
$nextID = $nameRow['MAX(ID)'] + 1;
//Set Blogname
$blogName = md5($nextID).'.txt';
$blogTargetFile = './file/blog/'.$blogName;
$fileHandle = fopen($blogTargetFile, 'w');
fwrite($fileHandle, $content);
fclose($fileHandle);
$sql2 = "INSERT INTO `blog` (`userID`, `name`, `created`, `featured`, `projectID`) VALUES ('".$userID."', '".$blogName."', '".$date."', '".$featured."', '".$projectID."')";
break;
case internalUpdate;
$sql1 = "SELECT * FROM `blog` WHERE `projectID` = ".$projectID;
$query1 = mysql_query($sql1);
$row1 = mysql_fetch_array($query1);
$blogName = $row1['name'];
$blogTargetFile = './file/blog/'.$blogName;
$fileHandle = fopen($blogTargetFile, 'w');
fwrite($fileHandle, $content);
fclose($fileHandle);
$sql2 = "UPDATE `blog` SET `created` = '".$date."' WHERE `ID` = ".$row1['ID'];
break;
}
mysql_query($sql2);
};
一个想法?
查尔斯 - richini-design.co.uk
最佳答案
不确定我是否正确,但由于 fopen 函数使用了“w”模式,您正在覆盖该文件。
尝试使用 fopen([filename], "a")
附加数据。
请参阅http://fr.php.net/manual/en/function.fopen.php了解更多信息。
关于PHP txt 文件编辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884860/