...

View Full Version : read and write



Mihai
03-04-2009, 03:57 AM
echo '

<?php
$what = $_GET['page'];

?>
<font face="Georgia">Edit <?php echo $what; ?></font>


<?php
$loadcontent = "test.php";
if($save_file) {
$savecontent = stripslashes($savecontent);
$fp = @fopen($loadcontent, "w");
if ($fp) {
echo 'written';
fwrite($fp, $savecontent);
fclose($fp);
}
}
$fp = @fopen($loadcontent, "r");
$loadcontent = fread($fp, filesize($loadcontent));
$loadcontent = htmlspecialchars($loadcontent);
fclose($fp);

?>
<form method=post action="<?=$_SERVER['PHP_SELF']?>">
<textarea name="savecontent" cols="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">
</form>
<p><a href="java script:history.go(-1)">Back</a></p>





</php>

'

i've been tryiing 666 up to 777 to chmod test.php (who reside in same folder)
:confused:
Any ideas ?
Thank you

abduraooft
03-04-2009, 05:59 AM
i've been tryiing 666 up to 777 to chmod test.php (who reside in same folder) Sorry, your intention/issue is not clear.

Mihai
03-04-2009, 10:52 PM
it reads the file but it does now write back after is edited

Mihai
03-05-2009, 12:05 AM
<?php

$action=$_POST['action'];
$textarea=$_POST['textarea'];

// Name of the page or text file to edit
$page="test.txt";

// Where to return
$return="edit.php";

// Get page
$data = implode("", file($page));

if($action=="save"){
$newtext=stripslashes($textarea);
$newtext = str_replace("<?", "", $newtext);
$newtext = str_replace("?>", "", $newtext);
$newtext = nl2br($newtext);
$fh = fopen($page, 'w') or die("can't open file");
fwrite($fh, $newtext);
fclose($fh);
header ("location: edit.php");
}
else{
echo"
<html>
<head><title>Simple Text Editor</title>
<style>
body,html{
margin:0px auto;
width:700px;
text-align:center;
}
#content{
margin:0px auto;
width:700px;
}
</style>
<body>
<div id='content'>
";
$ta=br2nl($data);

echo"
<a href='$return'><-- Return to Page</a><br /><br />
Make Changes and click \"Save Changes\" at the very bottom ...<br />
<form action='edit.php' method='post'>
<input type='hidden' name='action' value='save'>
<textarea name='textarea' rows='25' cols='80'>$ta</textarea>
<br />
<input type='submit' name='submit' value='Save Changes'>
</form>
</div>
</body>
</html>
";
}

function br2nl($str) {
return preg_replace('=<br */?>=i', "", $str);
}

?>

<?php
// Open the search file
$file = fopen($filename, 'r');

// Get rid of all PHP code.
$search = array('/<\?((?!\?>).)*\?>/s');

$text = fread($file, filesize($filename));

$new = strip_tags(preg_replace($search, '', $text));

echo $new;

fclose($file);
?>




now it works
but i have a diferent question ;)
is I use :



// Get rid of all PHP code.
$search = array('/<\?((?!\?>).)*\?>/s');

$text = fread($file, filesize($filename));

$new = strip_tags(preg_replace($search, '', $text));

echo $new;

fclose($file);



will strip the php code from the file but i want to strip the code only for editing only but when i save it back to stil have the code.

Can someone help me please

Mihai

Fou-Lu
03-05-2009, 12:11 AM
Display the data using htmlspecialchars. This will convert < and other special characters to a non-breaking representation that should be usable in a textarea. Saving will require not alterations, since the post method should treat it as the literal entity.

Mihai
03-05-2009, 12:34 AM
i just want to strip ( or block from displaying the html or php tags but not to remove permanently from the code ).
Just to have the code on my edit.php without <whatever> </whatever> , but when i save the file to stil have the : <whatever> </whatever> in it.

'<' (less than) becomes '&lt;'

'>' (greater than) becomes '&gt;'

Fou-Lu
03-05-2009, 12:50 AM
i just want to strip ( or block from displaying the html or php tags but not to remove permanently from the code ).
Just to have the code on my edit.php without <whatever> </whatever> , but when i save the file to stil have the : <whatever> </whatever> in it.

'<' (less than) becomes '&lt;'

'>' (greater than) becomes '&gt;'

Did you not read my last post? Display the data in a text area by using htmlspecialchars. Entities are replaced by the representation into a text area, but are not actually the character they represent. They will automatically convert back to the correct entity once the form has been submitted.
Make sure you're handling the magic_quotes correctly though.



if (isset($_POST['textArea']))
{
if (@get_magic_quotes_gpc())
{
$_POST['textArea'] = stripslashes($_POST['textArea']);
}
// File writing stuffs
}


I'd also make sure that the set_magic_quotes_runtime has been set to zero to prevent incoming data from being escaped.


Wait a minute, I don't think I quite got this right. You want to remove it completely from the display? In that case, you're asking too much from PHP. To do what you're asking is to edit just the data within it, use a flat file or a database to store the data and read and write only that file, not the code file.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum