|
-
Jul 4th, 2005, 07:20 PM
#1
Thread Starter
Junior Member
Read File
How can I read whats in a text file and display it on the page. Say the text file looks like:
This
is
a
test
of
the
file
How can I display it on the website? Also is there a way i can delete a certain row such as the word test so the text file would look like:
like:
This
is
a
of
the
file
Thanks
-
Jul 4th, 2005, 07:28 PM
#2
Re: Read File
I dont know how to delete a word, but id presume youd use an if statement with file()..but anyways this will read a file
PHP Code:
$filename = "yay.txt";
$Handle = fopen($filename, "r") or die("error opening");
$Contents =fread($handle,filesize($filename));
fclose($handle);
echo($contents);
-
Jul 4th, 2005, 07:36 PM
#3
Thread Starter
Junior Member
Re: Read File
Warning: fread(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 4
Warning: fclose(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 5
-
Jul 4th, 2005, 08:02 PM
#4
Re: Read File
my bad
PHP Code:
<?php
$filename = "visitorcount.txt";
$Handle = fopen($filename, "r") or die("error opening");
$Contents = @fread($Handle,filesize($filename));
@fclose($Handle);
echo($Contents);
?>
-
Jul 4th, 2005, 08:14 PM
#5
Fanatic Member
Re: Read File
try this:
NOTE: this is untested!
PHP Code:
<?php if($_POST['B1']){
$filedata = file('file.txt');
$i = 0;
foreach ($filedata as $line_num => $linedata) {
if($_POST["C$line_num"] == 0){
$filereturn[$i] = $linedata;
$i++;
}
}
$handle = fopen("file.txt", 'a') || die("cannot write to file");
fwrite($handle, implode("\n", $filereturn)) || die("another problem writing to file");
fclose($handle);?><body>
<form method="POST" action="?>echo $_SERVER['PHP_SELF']; ?>">
<? $filedata = file('file.txt');
foreach ($filedata as $line_num => $linedata) {
echo htmlspecialchars($linedata) . '<input type="checkbox" name="C' . $line_num . '" value="ON"><br />' . "\n";
}?><input type="submit" value="Delete" name="B1"></form>
</body>
-
Jul 4th, 2005, 08:34 PM
#6
Thread Starter
Junior Member
Re: Read File
Parse error: parse error, unexpected $ in /home/mcjedi/public_html/test.php on line 19
^^ ALLs, code
Last edited by McJedi; Jul 4th, 2005 at 08:38 PM.
-
Jul 5th, 2005, 01:37 AM
#7
Re: Read File
Code:
?>echo $_SERVER['PHP_SELF']; ?>>
i think the ? and the > got twisted. .. but I didn't test the whole thing.. just looked it over. perhaps i'm wrong.
-
Jul 5th, 2005, 09:16 AM
#8
Fanatic Member
Re: Read File
ya, that was one error, but i also forgot about a closing brackett... this "should" work:
PHP Code:
<?php if($_POST['B1']){
$filedata = file('file.txt');
$i = 0;
foreach ($filedata as $line_num => $linedata) {
if($_POST["C$line_num"] == 0){
$filereturn[$i] = $linedata;
$i++;
}
}
$handle = fopen("file.txt", 'a') || die("cannot write to file");
fwrite($handle, implode("\n", $filereturn)) || die("another problem writing to file");
fclose($handle);
}?><body>
<form method="POST" action="<?echo $_SERVER['PHP_SELF']; ?>">
<? $filedata = file('file.txt');
foreach ($filedata as $line_num => $linedata) {
echo htmlspecialchars($linedata) . '<input type="checkbox" name="C' . $line_num . '" value="ON"><br />' . "\n";
}?><input type="submit" value="Delete" name="B1"></form>
</body>
-
Jul 5th, 2005, 02:52 PM
#9
Thread Starter
Junior Member
Re: Read File
Ok it brings up check boxes with whats in the file and has a delete button, but when you check what you want to delete and hit delete you get this:
Warning: fwrite(): supplied argument is not a valid stream resource in /home/mcjedi/public_html/test.php on line 11
another problem writing to file
-
Jul 6th, 2005, 02:49 AM
#10
Re: Read File
You can use the file() function to load each line of the file into an array. To remove a particular line unset the array element corresponding to that line.
PHP Code:
<?php
$file = 'test.txt';
/* the file function loads each line of the file into an indexed array */
$file_array = file($file);
/* each line of the file is represented by an array element line 1 is index 0 */
unset($file_array[2]); // removes line 3
?>
<html>
<head>
<title>File Dump</title>
</head>
<body>
<?php foreach ($file_array as $line): // output the file by looping through the array ?>
<p><?php echo (htmlspecialchars($line)) ?></p>
<?php endforeach; ?>
</body>
</html>
-
Jul 6th, 2005, 03:15 AM
#11
Re: Read File
Extending the above example you can adelete selected lines from the file:
PHP Code:
<?php
$file = 'test.txt';
/* the file function loads each line of the file into an indexed array */
$file_array = file($file);
if (isset($_POST['delete'])) { // if the user pressed the submit button this variable will be set
if (isset($_POST['lines']) && is_array($_POST['lines'])) { // the variable is an arrway if the users selected lines to delete
/* attempt to open file for writing - we will overwrite the entire file
with only the lines the user has not selected to deelte */
if (! $fhwnd = fopen($file, 'wb')) {
die('Cannot open file for writing.');
}
/* go through each line and rewrtite it to the file only if
it is not present in the lines array */
foreach ($file_array as $number => $line) {
if (! in_array($number, $_POST['lines'])) {
fputs($fhwnd, $line);
}
}
/* save and close the file */
fclose($fhwnd);
/* get the new file array to display */
$file_array = file($file);
}
}
?>
<html>
<head>
<title>File Dump</title>
</head>
<body>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']) ?>">
<?php foreach ($file_array as $number => $line): // output the file by looping through the array ?>
<p><input id="line<?php echo($number) ?>"
type="checkbox"
name="lines[]"
value="<?php echo($number) ?>" />
<label for="line<?php echo($number) ?>"><?php echo (htmlspecialchars($line)) ?></label>
</p>
<?php endforeach; ?>
<p><input type="submit" name="delete" value="Delete Select Lines" /></p>
</form>
</body>
</html>
-
Jul 6th, 2005, 08:41 AM
#12
Fanatic Member
Re: Read File
similer to my example, that i couldn't get all the bugs out, because i couldn't test it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|