Jump to content

Show files in page


ngreenwood6

Recommended Posts

I am trying to create a backend for my website.  I would like to be able to edit certain pages from my backend. I would like to know how to show the code on a page for a certain file so that I can edit the page from my site. If anyone can help it would be appreciated. If you need more info please let me know.

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/
Share on other sites

I have come up with this code:

 

<?php

$fopen = fopen("index.php", "rb");
$default = fread($fopen);
?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $PHP_SELF; ?>">
<input type="textarea" value="$default">
</form>
</html>

 

However, It is giving me an error "Warning: Wrong parameter count for fread() in C:\wamp\www\login\edit.php on line 4". At this point I was just trying to open the file into a text area. Also it is just displaying default in the textarea. Anyone help?

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607097
Share on other sites

Actually I have just come up with this:

 

<?php

$index = file_get_contents("index.php");


?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $PHP_SELF; ?>">
<textarea name="textarea"  cols="100" rows="100" value="<? echo $index; ?>">
</form>
</html>

 

It displays my index page in the textarea. However, I do not know how I would code it to save the changes. If anyone can help it is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607099
Share on other sites

<?php
$file_name = "index.php";
$index = file_get_contents("$file_name");
if(isset($_POST['submit'])){
file_put_contents($textarea, $file_name);
echo "page has been updated.";
} else {
?>
<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $PHP_SELF; ?>">
<textarea name="textarea" cols="100" rows="100" value="<?php echo $index; ?>"></textarea><br>
<input type="submit" value="submit"><input type="reset" value="reset">
</form>
</html>
<?php
}
?>

 

i think.

 

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607104
Share on other sites

Firstly, $PHP_SELF has long been depricated, don't use it.

 

<?php

if (isset($_POST['submit'])) {
 file_put_contents('index.php', $_POST['textarea']);
} else {

$index = file_get_contents("index.php");

?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="textarea"  cols="100" rows="100" value="<? echo $index; ?>">
<input type="submit" name="submit">
</form>
</html>
<?php
}
?>

 

Be aware that this is just an example. Allowing users to write to your index.php file like that is very insecure. You really want to have all this locked behind a login system of some sort at the very least.

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607108
Share on other sites

you missed a </textarea>

 

but this still doesn't work for me though. any ideas? maybe its my server?

 

@ngreen

<?php

if (isset($_POST['submit'])) {
  file_put_contents('index.php', $_POST['textarea']);
} else {

$index = file_get_contents("index.php");

?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="textarea"  cols="100" rows="100" value="<? echo $index; ?>"></textarea>
<input type="submit" name="submit">
</form>
</html>
<?php
}
?>

 

is the correct code

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607118
Share on other sites

Sorry, should have picked this up. There is no such thing as a value attribute for a textarea. You'll also want to use htmlentities().

 

<?php

if (isset($_POST['submit'])) {
 file_put_contents('index.php', $_POST['textarea']);
} else {

$index = file_get_contents("index.php");

?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="textarea"  cols="100" rows="100"><?php echo htmlentities($index); ?></textarea>
<input type="submit" name="submit">
</form>
</html>
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607143
Share on other sites

Thanks this works for me. Now I have another quick question. After it is submitted I would like for it to go back to the same page but show it with the updated information. Is this the correct code?

 

<?php

if (isset($_POST['submit'])) {
 file_put_contents('test.php', $_POST['textarea']);
 echo "<a href='edit.php'>Go Back</a>";
} else {

$index = file_get_contents("test.php");

?>

<html>
<title>Read Data</title>
<form name="change" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<textarea name="textarea"  cols="80" rows="20"><?php echo htmlentities($index); ?></textarea>
<input type="submit" name="submit">
</form>
</html>
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/118013-show-files-in-page/#findComment-607158
Share on other sites

Archived

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

×
×
  • 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.