Why does it only write the first line to the file???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Why does it only write the first line to the file???

Post by andylyon87 »

Ok guys got it to write to file but now it only writes one line of the file, the top line to the file.

Code: Select all

function writefile($editing){if(!empty($_POST['save'])){
  $TheFile = 'form_data/'.$_POST['save'].'.txt'; 
  $Open = @fopen($TheFile, 'w'); 
  if($Open === FALSE){ 
  } else {
    fputs($Open, $editing);
    fclose($Open); 
  }
}}
print("<table border=0 width=450 cellspacing=2 cellpadding=2 align=center>");
print("<TR><TD colspan=5>");
if($save){
$c1=writefile("$editing");
if($c1)
echo '<tr><td colspan="5" align"center">Error occured during processing please go back and retry.</td></tr>';
}else{
echo '<tr><td colspan="5" align"center">file created/updated.</td></tr>';}

Code: Select all

if($edit){
for($n = 0; $n<count($edit); $n++){
print("<TR><TD colspan=5>The file currently being edited is:<B> form_data/$edit[$n]</B>");
}}else{
print("<TR><TD colspan=5>No file currently being edited");
}

print("<form action=files.php method=post enctype=multipart/form-data>\n");
print("<TR><TD colspan=5><textarea name="editing" cols=60 rows=10 NOWRAP>");
if($edit){
for($n = 0; $n<count($edit); $n++){
         $TheFile = "form_data/$edit[$n]";
         $Open = fopen ($TheFile, "r+");
         if($Open){
         $data = file($TheFile);
               $getline = explode("\t",$data[$n]);
                print("$getline[0]\t$getline[1]");
                fclose($Open);
         }else{
print("Unable to open a file<BR>\n");
               }
}}
print("</TEXTAREA>
<TR><TD colspan=5>
<TR><TD colspan=5>Save As:<input type=text name="save" size=20>
The textarea is at the top of the screen and you are meant to be able to click on a file it opens in the textarea (editing), you then edit the file and resave it using the save field. However it only saves the first line of the file. The rest of the code works below is a copy of all the code. Bit messy but not bothered really cause it works.

Code: Select all

<HTML><HEAD><TITLE>Moderators Page</TITLE></HEAD><BODY bgcolor=white>
<?
function writefile($editing){if(!empty($_POST['save'])){
  $TheFile = 'form_data/'.$_POST['save'].'.txt'; 
  $Open = @fopen($TheFile, 'w'); 
  if($Open === FALSE){ 
  } else {
    fputs($Open, $editing);
    fclose($Open); 
  }
}}
print("<table border=0 width=450 cellspacing=2 cellpadding=2 align=center>");
print("<TR><TD colspan=5>");
if($save){
$c1=writefile("$editing");
if($c1)
echo '<tr><td colspan="5" align"center">Error occured during processing please go back and retry.</td></tr>';
}else{
echo '<tr><td colspan="5" align"center">file created/updated.</td></tr>';}



if($Upload){
print("<TR><TD colspan=4><CENTER>Uploaded file name:$File_name</TD></TR>");
print("<TR><TD colspan=4><CENTER>Uploaded file size:$File_size</TD></TR>");
if(copy($File, "form_data/$File_name")){
print("<TR><TD colspan=4><CENTER>Your file, $File_name, was successfully uploaded!</TD></TR>");
}else{
print("<TR><TD colspan=4>Your file, $File_name, could not be copied.</TD></TR>");
}
unlink($File);
print("<TR><TD colspan=4><CENTER>&nbsp;<TD></TR>");
}

if($Rename){
for($n = 0;$n < count($Rename); $n++){
$OldFilename = $Rename[$n];
$Old = "form_data/$OldFilename";
$New = "moderated/$OldFilename";
if(rename("$Old", "$New")){
print("<TR><TD colspan=4><CENTER>Your file $Rename[$n], was successfully moved!</TD></TR>");
}else{
print("<TR><TD colspan=4><CENTER>Your file, $Rename[$n], could not be moved.</TD></TR>");
}
}
print("<TR><TD colspan=4><center>&nbsp;</TD></TR>");
}

if($Delete){
for($i = 0;$i < count($Delete); $i++){
if(unlink("form_data/$Delete[$i]")){
print("<TR><TD colspan=4><CENTER> Your file $Delete[$i], was successfully deleted!</TD></TR>");
}else{
print("<TR><TD colspan=4><CENTER>Your file, $Delete[$i], could not be deleted.</TD></TR>");
}
}
print("<TR><TD colspan=4><CENTER>&nbsp;</TD></TR>");
}

if($edit){
for($n = 0; $n<count($edit); $n++){
print("<TR><TD colspan=5>The file currently being edited is:<B> form_data/$edit[$n]</B>");
}}else{
print("<TR><TD colspan=5>No file currently being edited");
}

print("<form action=files.php method=post enctype=multipart/form-data>\n");
print("<TR><TD colspan=5><textarea name="editing" cols=60 rows=10 NOWRAP>");
if($edit){
for($n = 0; $n<count($edit); $n++){
         $TheFile = "form_data/$edit[$n]";
         $Open = fopen ($TheFile, "r+");
         if($Open){
         $data = file($TheFile);
               $getline = explode("\t",$data[$n]);
                print("$getline[0]\t$getline[1]");
                fclose($Open);
         }else{
print("Unable to open a file<BR>\n");
               }
}}
print("</TEXTAREA>
<TR><TD colspan=5>
<TR><TD colspan=5>Save As:<input type=text name="save" size=20>
<TR><TD><B>File Name
<TD><B>File Size
<TD><B>Delete
<TD><B>Move to Moderated
<TD><B>Edit");

$open = opendir ("form_data");
while($Files = readdir($open)){
$Filename = "form_data/" . $Files;
if(is_file($Filename)){
$Size = filesize("form_data/$Files");
print("<TR><TD>$Files</TD>
<TD>$Size<TD><INPUT type=checkbox name="Delete[]" value=$Files>
<TD><Input type=checkbox name="Rename[]" value=$Files>
<TD><Input type=radio name=edit[] value=$Files>
");
}}
closedir($open);
print("<TR><TD colspan=4>&nbsp;
<TR><TD colspan=4 align=center><input type=checkbox name=Upload value=yes>Upload A File:<Input type=file name=File size=20></TD></TR>
<TR><TD colspan=4 align=center><input type=submit name=submit value=Submit!>
</TABLE>\n");
?>
</BODY></HTML>

feyd | swapped

Code: Select all

for

Code: Select all

tags.[/color]
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

cheers feyd forgot bout them
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

first off, writefile() doesn't return anything so $c1=writefile() is useless. Next, you may need to switch the fputs() in writefile() to fwrite(); Next, you're mixing register_global on and off styles, choose one. Lastly, you have very error prone html..
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

told you it was a mess, have edited somethin i got out a book which was nasty anyway, so its been hard tryin to clear up this is v0.000000001 ;)
Post Reply