Jump to content

Recommended Posts

I'm just getting back into PHP again, and I am trying to make a script that will edit a file -- easy enough eh? I guess its a little more complex than a normal one, but I still dont get why it's not working.

 

First off, it's not reading the actual contents of the file, and secondly its not even editing it either. The permission is set to 777, so I don't really get it. I was hoping maybe you guys could look at the code and tell me if you find anything wrong (which I'm pretty sure you will).

 

<?
error_reporting('E_ALL');
echo '<center>';

$edit = new edit();

class edit {
function edit(){}

function display ($type, $file, $input) {
	//if initial view
	if ($type == 'initial') {
		//display file
		echo fread($file, filesize($file))
			//display button to edit file
			.'<br /><br /><form action="edit.php?go=Edit" method="POST">'
			.'<input type="submit" value="Edit Page" /></form>';
	}

	//if edit page view
	else if ($type == 'Edit') {

		//if they've already been to the confirm page
		if (isset($_COOKIE['hasbeen'])) {

			//display textbox with user input in it
			echo '<form action="edit.php?go=confirmpage" method="POST">'
			.'<textarea name="input">'
			//user input
			.$_COOKIE['input']
			.'</textarea><br />'
			.'<br /><input type="submit" value="Edit Page" /></form>';
			setcookie('hasbeen', '', time() - 3600);
		}

		//if they haven't been to the confirm page
		else {

		//display textbox with file contents in it.
		echo '<form action="edit.php?go=confirmpage" method="POST">'
			.'<textarea name="input">'
			//file input
			.fread($file, filesize($file))
			.'</textarea><br />'
			.'<input type="submit" value="Edit Page" /></form>';
		}
	}

	//if confirm page view
	else if ($type == 'confirmpage') {

		//set cookie that says they've been to this page
		setcookie('hasbeen', 'yes');

		//set the $input as a cookie so it's easily accessed
		setcookie('input', $input);

		//display user data input (non-textarea)
		echo 'This is what you have submitted:<br /><br />'
			.'<b>File:</b><br />'
			.$input.'<br /><Br />'

			//displays submit, cancel, and edit buttons
			.'<form action="edit.php" method="GET">'

			//cancel button
			.'<input type="submit" value="Cancel" name="go" />'

			//edit button
			.'<input type="submit" value="Edit" name="go" />'

			//confirm button
			.'<input type="submit" value="Confirm" name="go" />'
			.'</form>';
	}


	//edit the file
	else if ($type == 'Confirm') {
		//write
		fwrite($file, $input);

		//if failure
		if (fread($file, filename($file)) !==  $input) {
			echo '<b>Failure</b><br /><br />';
			$this->display('initial', $file, '');

		//if success
		} else {
			echo '<b>Success</b><br /><br />';
			$this->display('initial', $file, '');
		}
	}
	//Cancel Page
	else if ($type == 'Cancel') {
		setcookie('hasbeen', 'yes', time() - 3600);
		$this->display('initial', $file, $_POST['input']);
	}
	setcookie('hasbeen', '', time() - 3600);
}	
}




$file = fopen('file.php', 'a+');

//if new visitor
if (!$_POST && !$_GET) {
$edit->display('initial', $file, '');
}

//if go (GET) function is intiated
else if (isset($_GET['go'])) {
//go to correct display page
$edit->display($_GET['go'], $file, $_POST['input']);
}



?>

Link to comment
https://forums.phpfreaks.com/topic/64158-editing-a-file/
Share on other sites

Hi there,

 

I'm actually a real idiot when it comes to file handling, so forgive me for any dumb statements I may make here!  :(

 

Where have you declared the value for $type? Apart from the function, I cannot see where you have set it and your conditional statements revolve around this variable.

 

Also, can you try echoing your cookie data at the end of the page once the form has been submitted, that way you can confirm that they're not empty.

 

Just a thought,

Iceman

Link to comment
https://forums.phpfreaks.com/topic/64158-editing-a-file/#findComment-320146
Share on other sites

Well

$type

is just a part of the function so that when I call upon it, I can declare what I want it to do. So if I type go to

display('initial');

it would be different than

display('error');

for example.

 

I have checked the cookies, and I know they are being filled as well.

Link to comment
https://forums.phpfreaks.com/topic/64158-editing-a-file/#findComment-323164
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.