Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: read and write

  1. #1
    Regular Coder Mihai's Avatar
    Join Date
    Jan 2008
    Posts
    122
    Thanks
    14
    Thanked 0 Times in 0 Posts

    read and write

    PHP Code:
    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($fpfilesize($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)

    Any ideas ?
    Thank you
    Last edited by Mihai; 03-04-2009 at 02:59 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    i've been tryiing 666 up to 777 to chmod test.php (who reside in same folder)
    Sorry, your intention/issue is not clear.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder Mihai's Avatar
    Join Date
    Jan 2008
    Posts
    122
    Thanks
    14
    Thanked 0 Times in 0 Posts
    it reads the file but it does now write back after is edited

  • #4
    Regular Coder Mihai's Avatar
    Join Date
    Jan 2008
    Posts
    122
    Thanks
    14
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?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($filefilesize($filename));

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

    echo 
    $new;

    fclose($file);
    ?>

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

    PHP Code:
    // Get rid of all PHP code.        
    $search = array('/<\?((?!\?>).)*\?>/s');
            
    $text fread($filefilesize($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

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder Mihai's Avatar
    Join Date
    Jan 2008
    Posts
    122
    Thanks
    14
    Thanked 0 Times in 0 Posts
    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;'

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by Mihai View Post
    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.

    PHP Code:
    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.

    Edit:
    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.
    Last edited by Fou-Lu; 03-04-2009 at 11:55 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •