简体   繁体   中英

log and write with php?

well i have this php to log and write my text from a textarea but its only logging. Help?

<?php 
$fn = "test.txt"; //the path to any file

if (isset($_POST['content'])) 
{ 
    $content = stripslashes($_POST['content']); 
    $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); 
    fputs($fp,$content); 
    fclose($fp) or die ("Error closing file!"); 
} 
?> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        @import "Stickies.css";
    </style>
</head>
<body>
<div id="practiceDiv"></div>
<div id="front">
    <img src='Images/yellow.png' id='frontImg'/>    
    <img src="Images/notAllowed.png" id="dont"/>
    <form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post">
    <textarea name="txt" style="font-family: Marker Felt; font-size: 16px; background-color: transparent; border: none; height:159px; resize: none;" id="myDiv" onkeydown="handleTyping(event);" onkeyup="handleKeyUp(event);" onpaste="handlePaste(event);" name="content"><?php readfile($fn); ?></textarea>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
    <input type="submit" value="Save"> 
<?php
$x = $_POST['txt'];
?> 
</form>
</div>
</body>
</html>
<?
$string = $x
?>
<?

$filename = 'log.txt';
$fp = fopen($filename, "a+");
$write = fputs($fp, $string . "\r\n");
fclose($fp);

?>

fixed your example, hope it helps

<?php 
$fn = "test.txt"; //the path to any file

//get posted content from the textarea
if (isset($_POST['txt'])) 
{ 
    $content = stripslashes($_POST['txt']); 
    $fp = fopen($fn,"w+") or die ("Error opening file in write mode!"); 
    fputs($fp,$content); 
    fclose($fp) or die ("Error closing file!"); 
} 
?> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        @import "Stickies.css";
    </style>
</head>
<body>
<div id="practiceDiv"></div>
<div id="front">
    <img src='Images/yellow.png' id='frontImg'/>    
    <img src="Images/notAllowed.png" id="dont"/>
    <form action="" method="post">
    <!-- removed the name="content"-->
    <textarea name="txt" style="font-family: Marker Felt; font-size: 16px; background-color: transparent; border: none; height:159px; resize: none;" id="myDiv" onkeydown="handleTyping(event);" onkeyup="handleKeyUp(event);" onpaste="handlePaste(event);" ><?php readfile($fn); ?></textarea>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
        <br/>
    <input type="submit" value="Save"> 

</form>
</div>
</body>
</html>

<!--your php short tag may not be supported-->
<?php
$filename = 'log.txt';
$fp = fopen($filename, "a");
//used $content var instead of making new one
fputs($fp, $content . "\r\n");
fclose($fp);

?>

people usually prefer file_get_contents and file_put_contents over fputs & fgets ,

it saves us some lines of code, and more simplicity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM