[PHP] File editing not working X_X
My friend asked me to write a review script for his website. He doesn't have MySQL which made it harder than usuall, but so far it works. Today I started working on the editing the reviews part of it and got stumped. Here's the code:
<?php
function Edit($id) {
global $globals;
Output(\"header2\",\"Edit File\",FALSE,\"\");
if (!isset($id)) {
echo \"You must specify a review to edit! Please go back and select one!\";
} else {
$blah = $globals[\"dir\"].\"/\".$id.\".\".$globals[\"ext\"];
$fp = fopen($blah, \"r\");
$file = fread($fp,filesize($blah));
fclose($fp);
$contents = explode(\"///--///\",$file);
$review = str_replace(\"|br|\",\"\",$contents[\"5\"]);
$title = $contents[\"4\"];
echo<<<MyForm
<form action=\"reviews.php\" method=\"post\">
<input type=\"hidden\" name=\"doedit\" value=\"do\">
<input type=\"hidden\" name=\"id\" value=\"$id\">
<b>Review:</b> <input type=\"text\" name=\"title\" value=\"$title\"><br>
<b>Contents:</b><br>
<textarea cols=\"50\" rows=\"15\" name=\"review\">$review</textarea><br>
Password: <input type=\"password\" name=\"password\"><br>
<input type=\"submit\">
<input type=\"reset\"></form>
MyForm;
}
Output(\"footer\",\"\",FALSE,\"\");
}
function DoEdit() {
global $globals;
$id = $_POST[\"id\"];
$blah = $globals[\"dir\"].\"/\".$id.\".\".$globals[\"ext\"];
echo $blah.\"<br>\";
$fp = fopen($blah,\"r\");
$file = fread($fp,filesize($blah));
fclose($fp);
$file = explode(\"///--///\",$file);
$password = md5($_POST[\"password\"]);
if ($password != $file[\"2\"]) {
Output(\"header2\",\"Edit File\",FALSE,\"\");
echo \"Error! Passwords do not match!\";
Output(\"footer\",\"\",FALSE,\"\");
exit;
}
$review = str_replace(\"\n\",\"|br|\",$_POST[\"review\"]);
$review = $review.\"|br||br|<i>Last Edited:\".date (\"F dS, l, Y at h:i:s A\");
$new_file = $id.\"///--///\".$file[\"1\"].\"///--///\".$file[\"2\"].\"///--///\".$file[\"3\"].\"///--///\".$_POST[\"title\"].\"///--///\".$review.\"///--///0\";
$fp = fopen($blah,\"w+\");
if (fwrite($fp,$new_file)) {
Output(\"header2\",\"Edit File\",TRUE,$globals[\"url\"]);
echo \"Updated!\";
Output(\"footer\",\"\",FALSE,\"\");
} else {
Output(\"header2\",\"Edit File\",FALSE,\"\");
echo \"Error! File was not updated!\";
Output(\"footer\",\"\",FALSE,\"\");
exit;
}
fclose($fp);
}
?>
When DoEdit() runs it's just blank. If I add a foreach() to echo the form values it echos them fine.
Script: http://necrotic.anitrade.net/reviews.php
To add a review: http://necrotic.anitrade.net/reviews.php?submit=new
Any help would be much appreciated!
[James Logsdon]
necrotic posted this at 20:16 — 22nd May 2003.
He has: 296 posts
Joined: May 2002
O yes, the file format is:
id///--///ip///--///password///--///author///--///title///--///review///--///rating
Mark Hensler posted this at 21:39 — 22nd May 2003.
He has: 4,048 posts
Joined: Aug 2000
I'm sorry, I'm half conscious right now.
Why are you using a flat file as opposed to a mysql db?
necrotic posted this at 22:49 — 22nd May 2003.
He has: 296 posts
Joined: May 2002
Mark Hensler posted this at 05:26 — 23rd May 2003.
He has: 4,048 posts
Joined: Aug 2000
I'm sorry. I'm not quite myself today. (had 4 wisdom teeth pulled 12 hours ago, and I'm still light headed and now very sleepy)
I'm guessing that DoEdit() is not running at all. Where are you calling it from? You should having something like:
if ($doedit=='do') {
DoEdit();
}
Mark Hensler
If there is no answer on Google, then there is no question.
necrotic posted this at 10:50 — 23rd May 2003.
He has: 296 posts
Joined: May 2002
And I do. All that happens is a blank page shows up.
Mark Hensler posted this at 17:43 — 23rd May 2003.
He has: 4,048 posts
Joined: Aug 2000
can you paste those lines here plz
necrotic posted this at 19:48 — 23rd May 2003.
He has: 296 posts
Joined: May 2002
<?php
if ((!isset($id))&&(!isset($submit))&&(!isset($edit))&&(!isset($doedit))) Main();
elseif ((isset($id))&&(!isset($submit))&&(!isset($edit))&&(!isset($doedit))) Display($id);
elseif ((!isset($id))&&($submit==\"new\")&&(!isset($edit))&&(!isset($doedit))) Submit();
elseif ((!isset($id))&&($submit==\"do\")&&(!isset($edit))&&(!isset($doedit))) DoSubmit();
elseif ((!isset($id))&&(isset($edit))&&(!isset($submit))&&(!isset($doedit))) Edit($edit);
elseif ((!isset($id))&&($doedit==\"do\")&&(!isset($edit))&&(!isset($submit))) DoEdit();
?>
[James Logsdon]
Mark Hensler posted this at 00:19 — 24th May 2003.
He has: 4,048 posts
Joined: Aug 2000
logic error
$id is set, and you have !isset($id)
necrotic posted this at 03:11 — 24th May 2003.
He has: 296 posts
Joined: May 2002
The thing is, though, is that if I tell the function DoEdit() to echo say, all the form values, it does it fine. When I get the chance (tomorrow) I'm going to have it echo $new_file and see if the values are set correctly.
[James Logsdon]
necrotic posted this at 20:02 — 24th May 2003.
He has: 296 posts
Joined: May 2002
Well, turns out that when I change the if statements controlling the functions it works. Thanks for giving me the idea of rewriting it, Mark! Now I can do the last 2 things: a small rating system and a logging function and I'll be done!
[James Logsdon]
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.