Page 1 of 1
File Append
Posted: Sun May 25, 2008 7:21 pm
by emenard
Hello All,
I'm very new to PHP, and I could use some help figuring out a PHP problem I'm having. I need to have a php file dump data into a .txt file using a "file append" approach, then output images to the same php file. So far, I have a this working with outputting plain old text, but I'm not sure how to get the text file to output an image. Any help would be appreciated!
Thanks,
Liz
Re: File Append
Posted: Sun May 25, 2008 10:43 pm
by emenard
It may help if I post my code: In short, I'm wondering how to get an image to display instead of text.
Thanks!
Liz
<?php
$myFile = "003_phpread_example.txt";
$addedline = "";
if(isset($_REQUEST['submit-it'])) {
$stringData = $_REQUEST['filename'] . $_REQUEST['delimiter'] . $_REQUEST['comment'] . "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
$addedline = "<p><b>****We added a line.***</b></p>";
}
?>
<html>
<head>
<title>read write example</title>
<style>
fieldset { width: 500px; border: 1px solid #888888; padding: 12px 12px 24px 12px; margin: 24px; text-align: left; }
legend { background-color: #e4e4f4;border: 1px solid #888888; padding:6px;}
div { background-color: #e4e4f4; }
</style>
</head>
<body>
<center>
<!-- Show added line notification, if we have one. -->
<?php
if ($addedline != "") {
echo '<div>';
echo $addedline;
echo '</div>';
}
?>
<fieldset>
<!-- <legend>Add It</legend> -->
<!-- <p>Here, we'll add stuff to the file.</p>
-->
<form method="post" action="<?=$PHP_SELF?>">
<p>Filename (first field):
<input type="text" name="filename" size="36" value="savedoc-#.pdf"/></p>
<p>Comment (second field):
<input type="text" name="comment" size="36" value="This is my comment for doc #"/></p>
<!-- <p>Delimiter (don't change):
<input type="text" name="delimiter" size="4" value="||"/></p> -->
<p><input type="submit" name="submit-it" id="submit-it" value="submit"/></p>
</form>
</fieldset>
<fieldset>
<!-- <legend>Read It</legend> -->
<!-- <p>Grabbing the file, line by line, as is. Adding line numbers to the beginning.<br>
* Note that in nearly all coding, numbering starts with 0. It's a cardinal rule.
</p> -->
<?php
if (file_exists($myFile))
{
$lines = file($myFile); // Get a file into an array.
if (sizeof($lines) > 0) // If we have lines
{
foreach ($lines as $line_num => $line) // Loop through our array,
{
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
}
}
else
{
echo '<p>no file yet.</p>';
}
?>
</fieldset>
<fieldset>
<!-- <legend>Read It (with delimiters)</legend>
<p>Now, I'll start using the delimiter to seperate the data. In the text file I use "double pipes" aka ||<br>
The point of a delimiter is choosing something that a user won't type in. If they do, it'll mis-splice the data.
</p> -->
<?php
if (file_exists($myFile))
{
$lines = file($myFile); // Get a file into an array.
if (sizeof($lines) > 0) // If we have lines
{
echo '<table cellpadding="4" cellspacing="0" border="1">';
echo '<tr>';
echo ' <th>Line Number</th>';
echo ' <th>Filename</th>';
echo ' <th>Comment</th>';
echo '</tr>';
foreach ($lines as $line_num => $line) // Loop through our array,
{
$pieces = explode ("||", $line); // Splits the line by the delimiter I chose
// Remember I mentioned that looping starts with 0, it applies here
// The Filename is part 0
// The comment is part 1
echo '<tr>';
echo ' <td>' . $line_num . '</td>';
echo ' <td>' . $pieces[0] . '</td>';
echo ' <td>' . $pieces[1] . '</td>';
echo '</tr>';
}
echo '</table>';
}
}
else
{
echo '<p>no file yet.</p>';
}
?>
</fieldset>
</center>
</body>
</html>