Jump to content

fwrite doesn't want to work...


Cod-nes

Recommended Posts

So here's my code:The problem: $file3 and $file1 do not want to be written.

<?phpif(!$index) {die ("Sorry, you must go to: <a href=\"index.php?news=edit\">" . $_SERVER["HTTP_HOST"].dirname($_SERVER["REQUEST_URI"]) . "/index.php?news=edit</a>");}$fn=$_GET["filename"];$file=$_POST["file"];if($file){$gettext = $_POST["text"];$text8 = str_replace("<br>", "<br />", $gettext);$text9 = str_replace("[b]", "<b>", $text8);$text10 = str_replace("[/b]", "</b>", $text9);$text11 = str_replace("[i]", "<i>", $text10);$text12 = str_replace("[/i]", "</i>", $text11);$text13 = str_replace("[url]", "<a href=", $text12);$text14 = str_replace("[/url]", ">Link</a>", $text13);$text15 = str_replace("[hr]", "<hr>", $text14);$text16 = str_replace("[img=", "<img src=", $text15);$text17 = str_replace("]", ">", $text16);$text18 = str_replace("[color=", "<span style='color:", $text17);$text19 = str_replace("x]", "'>", $text18);$text20 = str_replace("[/color]", "</span>", $text19);$text21 = str_replace("", "<br /><br />", $text21);$text22 = str_replace("[mail]", "<a href='mailto:", $text21);$text23 = str_replace("[/mail]", "'>Email</a>", $text22);$text24 = str_replace("[size=", "<span style='font-size:", $text23);$text25 = str_replace("xx]", "'>", $text24);$text26 = str_replace("[/size]", "</span>", $text25);$text27 = stripslashes($text26);$old=include($file);$file1 = fopen($file,"w");fwrite ($file1,$text27);fclose($file1);$to=$email;$subject="Website Information: Edited News";$headers="From: no-reply@site.com";$message="You have just edited a page on \nhttp://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . ".\nUpdated on " . gmdate("l dS \of F Y h:i:s A") . "\nFile:" . $file . "\n\nNews Contexts:" . $text27 . "Old Contexts:" . $old . "\nThank you for using Aqua News.\nCopyright 2008";mail($to,$subject,$message,$headers);echo "Page " . $file . " edited. <a href='index.php'>Home</a>";}else{$gettext = $_POST["text"];$text8 = str_replace("<br>", "<br />", $gettext);$text9 = str_replace("[b]", "<b>", $text8);$text10 = str_replace("[/b]", "</b>", $text9);$text11 = str_replace("[i]", "<i>", $text10);$text12 = str_replace("[/i]", "</i>", $text11);$text13 = str_replace("[url]", "<a href=", $text12);$text14 = str_replace("[/url]", ">Link</a>", $text13);$text15 = str_replace("[hr]", "<hr>", $text14);$text16 = str_replace("[img=", "<img src=", $text15);$text17 = str_replace("]", ">", $text16);$text18 = str_replace("[color=", "<span style='color:", $text17);$text19 = str_replace("x]", "'>", $text18);$text20 = str_replace("[/color]", "</span>", $text19);$text21 = str_replace("", "<br /><br />", $text21);$text22 = str_replace("[mail]", "<a href='mailto:", $text21);$text23 = str_replace("[/mail]", "'>Email</a>", $text22);$text24 = str_replace("[size=", "<span style='font-size:", $text23);$text25 = str_replace("xx]", "'>", $text24);$text26 = str_replace("[/size]", "</span>", $text25);$text27 = stripslashes($text26);include ("file.php");$lf++;$file2 = fopen($lf . ".html","x");fwrite ($file2,$text27);fclose($file2);$file3 = fopen("file.php","w");fwrite ($file3,"\<?php \$lf=$lf; ?>");fclose($file3);$file4 = fopen("menu.php","a");fwrite ($file4,"\n<a href='index.php?news={$lf}'>{$fn}</a><br />");fclose($file4);echo "Page " . $file . " created. <a href='index.php'>Home</a>";$to=$email;$subject="Website Information: New News";$headers="From: no-reply@site.com";$message="You have just created a page on \nhttp://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . ".\nUpdated on " . gmdate("l dS \of F Y h:i:s A") . "\nFile:" . $lf . "\n\nContexts:" . $text27 . "\nThank you for using Aqua News.\nCopyright 2008";mail($to,$subject,$message,$headers);}

Link to comment
Share on other sites

Usually the problem is in fopen(). It doesn't look like you're checking the return value before trying to write to it, so you don't really know. Echoing your values is a basic debugging step.What goes wrong in fopen() is usually (1) a bad path or (2) the directory you're trying to write into doesn't have the correct permissions.FWIW, you can tighten up your code in a few ways.1. $text8 = str_replace("", "<b>", $text8) is legitimate syntax that keeps you from creating a gazillion variables2. str_replace() will take arrays as arguments, meaning you wouldn't have to call it over and over to make various replacements.

Link to comment
Share on other sites

I limited this to four values, but there are no real limits. Put them all in your arrays. Just make sure the search and replace values have the same index. The vertical syntax I'm using is legal in PHP and makes long array assignments more readable.

$a1 = array (   "<br>",   "[b]", "[/b]",   "[i]", "[/i]");$a2 = array (   "<br />",   "<b>", "</b>",   "<i>", "</i>");$s = "The fast [b]cat[/b] jumped over the [i]dog[/i].<br>";echo $s;$s = str_replace ($a1, $a2, $s);echo $s;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...