user3759758
user3759758

Reputation: 77

load files in a textarea on click using php?

I am using the following code in order to load a file into a textarea on my page. this works as it should. however, I need to be able to make changes to the loaded file and save it.

the problem that i have is that I can load the file in the textarea but i cannot save it and i keep getting this error: Error opening file in write mode!

This is my current code:

PHP code:

$fn = $_GET['p'];

if (isset($_POST['content']))

{

    $content = stripslashes($_POST['content']);

    $fp = fopen($fn,"w") or die ("Error opening file in write mode!");

    fputs($fp,$content);

    fclose($fp) or die ("Error closing file!");

}

HTML CODE:

<a href="?p=PAGE.html">PAGE.html</a>


<textarea id="code"  style="width:450px;"  rows="25" cols="50" name="content"><?php readfile($fn); ?></textarea>

The code above will load the PAGE.html into the textarea if you click on the <a></a> link, but it wont allow me to make changes to the file.

but if i change $fn = $_GET['p']; to $fn = 'PAGE.html';, everything works fine and i can make changes to the loaded file and save it without any errors.

could someone please advise on this issue?

Thanks

Upvotes: 0

Views: 81

Answers (1)

Kylie
Kylie

Reputation: 11749

Its because when you post the file, it's not appending the page name, so the $_GET variable will be empty when you submit it to save.

You have it as a $_GET variable when you click the link.

But when you post the form, you aren't posting with the $_GET variable anymore. So instead just include it in the form as a hidden field, and use $_REQUEST to get it regardless whether its a $_GET or $_POST

So like this...

$fn = $_REQUEST['page'];

and your html to this...

<a href="?page=PAGE.html">PAGE.html</a>


<input type="hidden" name="page" value="<?php echo $fn;?>" />
<textarea id="code"  style="width:450px;"  rows="25" cols="50" name="content"><?php readfile($fn); ?></textarea>

Upvotes: 1

Related Questions