Click to See Complete Forum and Search --> : Won't write out entire source!


Daniel T
04-19-2004, 11:48 PM
Hi. I'm currently working on a site manager script so I can update my website when I'm not at home. What I have so far is located here (http://216.36.173.149/site.php). Login to the Site Manager section with the username "user" and the password "pass". On the next page, click the "Open" button, and type "blog04.php" into the prompt window and click ok. Verify that the correct name is in the textbox, and then click "Open It!". It should take you to a page with an IFRAME containing blog04.php, and then a textarea containing the source of blog04.php. Scroll down so you can see the textarea. The first line reads:
<title>Daniel Tomasiewicz's Personal Website | Who's payin' these guys?!</title>
However, the first line, when I open it in DW, is the following, just like it should be:
<?php include('includes/preheader.xml'); ?>
I am using fopen() to open the file, and fread() to read it. It can't be a problem with it parsing the PHP before it outputs it, because if you scroll down to the bottom of the source, you'll see the PHP appears fine there. Any suggestions?? I'm stuck! Thanx,
-Dan

Nevermore
04-20-2004, 03:32 AM
If you view your source you'll see this line:

<textarea style='margin:20px; width: 90%; height: 300px;><?php include('includes/preheader.xml'); ?>


You missed the closing ' from the textarea style attribute, so the php code is being taken to be part of the tag.

Daniel T
04-20-2004, 12:26 PM
I should've noticed that!! Thanx man!
-Dan

Conor
04-20-2004, 06:54 PM
hmm i tride this but the code wont come up in the textarea what did i do wrong?

<form action="<?$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit">
<?
$file=$_POST['file'];
if($file != "")
{
$fopen= fopen($file,'w+');
$fread= fread($fopen, $file);
echo"<textarea>".$fread."</textarea>";
}
?>

Jona
04-20-2004, 07:11 PM
Originally posted by RefreshF5

<form action="<?$_SERVER['PHP_SELF'];?>" method="POST">


Try using


<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">

Conor
04-20-2004, 07:14 PM
no heres the error im getting

Warning: fread(): Length parameter must be greater than 0. in C:\Program Files\Apache Group\Apache2\htdocs\manage-remote.php on line 8


<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit">
<?
$file=$_POST['file'];
if($file != "")
{
$fopen= fopen($file,'w+');
$fread= fread($fopen, $file);
echo"<textarea>".$fread."</textarea>";
}
?>

Jona
04-20-2004, 07:21 PM
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit">
<?
$file=$_POST['file'];
if($file != "")
{
$fopen= fopen($file,'w+');
$fread= fread($fopen, filesize($file));
echo"<textarea>".$fread."</textarea>";
}
?>

Conor
04-20-2004, 07:24 PM
hmm, nope i get the same error.

Daniel T
04-20-2004, 07:28 PM
That error means that the file you are reading does not have any content.
-Dan

Jona
04-20-2004, 07:28 PM
You might want to close the file afterwards, and check to see if the variable isset before setting it to a new variable first to avoid errors, as well. ;)


<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit">
<?
if(isset($_POST["file"]))
{
$file = $_POST["file"];
if(filesize($file) > 0)
{
$fopen= fopen($file,'w+');
$fread= fread($fopen, filesize($file));
echo"<textarea>".$fread."</textarea>";
fclose($fopen);
}
}
?>

Daniel T
04-20-2004, 07:33 PM
If it helps, here is the code I use:
<?php
$thefile = $_POST['thefile2open'];
$openit = fopen($thefile, "r");
$existing = fread($openit, filesize($thefile));
fclose($openit);
print("<textarea>$existing</textarea>");

$open4write = fopen($thefile, "w+");
fputs("Whatever you want to write to the file.");
fclose($open4write);
?>
-Dan

Jona
04-20-2004, 07:40 PM
Daniel has a point, why not use the 'r' directive in your fopen() function, instead of the 'w+' one?

Conor
04-20-2004, 07:43 PM
because what about when i have to overwrite it, wont 'r' only read it?

Jona
04-20-2004, 07:46 PM
Originally posted by RefreshF5
because what about when i have to overwrite it, wont 'r' only read it?

Read it, close it, then when you need to overwrite it, open it, overwrite it, and close it again.

Conor
04-20-2004, 07:58 PM
hmm it was still throwing the same error so i tried another way of doing it and it does everything except actually overwrite the file?

<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit"><br>
<?php
$file=$_POST['file'];
$code= file_get_contents($file);

?>
<textarea rows=25 cols=75 name="edit"><?echo $code;?></textarea>
<input type="submit" name="overwrite" value="edit">
<?
$edit=$_POST['edit'];
if($edit != "")
{
$fopen= fopen($file , 'w+');
fwrite($fopen, $edit);
fclose($fopen);
$phrase="The File has been overwritten";
echo $phrase;
}
?>

Daniel T
04-20-2004, 09:18 PM
I've edited my post above to make it so you can write to the file. Also, regarding the code you just posted, try changing it to this:
<form action="<?=$_SERVER['PHP_SELF'];?>" method="POST">
<input type="text" name="file" value="Enter filename to edit"><input type="submit"><br>
<?php
$file=$_POST['file'];
$code= file_get_contents($file);

?>
<textarea rows=25 cols=75 name="edit"><?echo $code;?></textarea>
<input type="submit" name="overwrite" value="edit">
<?
$edit=$_POST['edit'];
if($edit != "")
{
if(!($fopen = fopen($file , 'w')))
{
echo "File could not be loaded";
}
else
{
fwrite($fopen, $edit);
fclose($fopen);
$phrase="The File has been overwritten";
echo $phrase;
}
}
?>

Conor
04-20-2004, 09:31 PM
hmm that gave me a weird error ive never seen before

Parse error: parse error, unexpected T_ELSE in C:\Program Files\Apache Group\Apache2\htdocs\manage-remote.php on line 18

Daniel T
04-20-2004, 09:38 PM
OK, my bad. I put a semicolon after the fopen statement when I shouldn't have. I edited my post, and it should fix the error.
-Dan

Jona
04-20-2004, 10:08 PM
Why are you using "w+"? You should be using just the "w" without the plus sign. ;)

Daniel T
04-20-2004, 10:09 PM
Originally posted by Jona
Why are you using "w+"? You should be using just the "w" without the plus sign. ;)
I was wondering this too, but I didn't know if it affected the outcome or anything, so I just left it alone.
-Dan

Jona
04-20-2004, 10:37 PM
I was doing a counter script once, and I had used this; I instead read it with 'r,' and then wrote it with 'w,' and it worked fine; otherwise it appended exactly as if I had used 'a+.'

Nevermore
04-21-2004, 02:01 PM
That's because you had moved the file pointer to the end of the file by reading it first. If you use w+ to read then write it will append, if you close and reopen the file or use rewind() it will overwrite.