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 13 of 13
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Editing PHP File

    Ok i've made this. Its to edit a whole php file. How can i change it to edit part of a file?
    THanks

    <?
    // Submission Part
    if ($submit) {
    $fp = fopen('YOURPAGE.php', 'w');
    fwrite($fp, stripslashes($newdata));
    fclose($fp);
    }

    $fp = fopen('YOURPAGE.php', 'r');
    while (!feof($fp)) {
    $data .= fgets($fp, 4096);
    }
    fclose($fp);
    echo "
    <html>
    <head>
    <title>PAGE</title>
    </head>

    <body>
    <form action='edit.php' method='post'>
    <textarea name='newdata' rows='20' cols='50'>
    $data
    </textarea>
    <br>
    <input type='submit' name='submit' value='Submit'>
    </form>
    </body>
    </html>";
    ?>

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Wht do you mean by "a part of the file"? The beginning, the end? a part from x to y or something else?
    BTW if you just want to print the whole file
    PHP Code:
    print file_get_contents($file); 
    is faster.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorta like this <bits inside this tag gets edited> blah blah blah! </end>

  • #4
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    does anyone undertsand?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Ohhh, I don't ever recommend editing actual php files. This will not only lead to confusion but also to frustrating problems.

    Offhand, I don't believe you can actually write from line x to line y. I'm not a huge file fan myself, but I don't recall being able to do this. In order to do these alterations, you'll need to read the file (Marek's code is an easy example!) into a variable, edit what you need using str_replace or regexp, and re-write the entire file.
    Now, if you were looking into using XML with DOM capabilities, this would be a whole 'nother story, as you can manipulate the elements as you see fit and redump into the xml file (HTML can be used the same way, but you'll need to create a DTD for use of some manipulations).
    Personally, I don't hardcode any HTML into my php scripts, so I don't even know offhand what your best route would be; other than creating a template system of course.

  • #6
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    does anyone know how to create form. you type in txt and it submits it to mysql database. you can then put this info on a page like this <?php echo $data. ''; ?>

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Php you'll need to make use of mysql functions for reading and writing capabilities.
    In order to provide you with examples, I'd need to know some information on database and page structures, datatypes, and other misc things.
    Depending on what data exists for $data ($newdata), you could very well use a text file. Just make it a new one and import it in as nessessary:
    PHP Code:
    <?php // Always use full tags, shorttags are an option
    $text_file './data.txt'// Current directory, file name data.txt (0644+ chmod)
    // Submission Part 
    if (isset($_POST['submit']))
    {
         if (
    is_writeable($text_file))
         {
              if (!
    $fp fopen($text_file'w+b'))
              {
                   echo 
    'Cannot open file for write!';
                   exit;
              }
              if (
    fwrite($fpstripslashes($_POST['newdata'])) === FALSE)
              {
                   echo 
    'Cannot write to file!';
                   exit;
              }
              
    fclose($fp);          
         }
    }

    $data file_get_contents($text_file);
         
    echo 

    <html> 
    <head> 
    <title>PAGE</title> 
    </head> 

    <body> 
    <form action='edit.php' method='post'> 
    <textarea name='newdata' rows='20' cols='50'> 
    $data 
    </textarea> 
    <br> 
    <input type='submit' name='submit' value='Submit'> 
    </form> 
    </body> 
    </html>"

    ?>
    This work for your needs?

  • #8
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i need to explain what i'm trying to do

  • #9
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok i have this file sql.php I want to be able to edit the bits that say 888 with a form
    <?php
    //DJ Settings

    //DJ Password - For DJs
    $djpass = '888';

    //Thank You / Thank You Message When They Semd Request
    $thankyou = '888';

    //Owner - For Hmepage
    $Owner = '8';

    //shoutcast Server Url -
    $shoutadmin = '888';

    //Master DJ / Management Password - To view complaints
    $mpass = '888';

    //MySQL Hostname
    $hostname = '888';

    //MySQL Database Username
    $sqlname = '888';

    //MySQL Database Password
    $sqlpass = '888';

    //MySQL Database Name
    $mysqldb = '888';

    $connection = mysql_connect($hostname, $sqlname, $sqlpass) or die("Couldn't select database.");
    $db = mysql_select_db($mysqldb, $connection) or die("Couldn't select database.");

    ?>

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts

    This can be done.
    However, since this is a configuation file I strongly recommend that you DO NOT ALLOW PHP TO EDIT THIS! Such files are hardcoded into websites for good reason.
    I can see reason for creating from scratch, which would be relatively simplistic and secure, but editing this file... your asking for trouble.
    For this, you would need someone far more skilled with filesystems than I, not to mention one whose foremost concern is security.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can you or someone tell me how to do it?
    Last edited by jabbic; 09-13-2005 at 07:59 PM.

  • #12
    New Coder
    Join Date
    Sep 2005
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if i cannot do it that way. Could i make a form to submit txt or coding to the bits that say 888

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'm assuming you are talking about a full php file correct?
    I won't provide you with any examples, as without knowing your site in and out and all security you are implimenting to contain these files, I cannot guarentee the effectiveness of the script.
    Doing it is a simple task. Simply read the entire contents into a string variable using a file reading function (fread, file_get_contents, readfile, etc), load the variable into a textarea using htmlspecialchars, and have an update write this new value to the file with stripslashes. That should do it for you, control the entire file from a textarea.
    I'm just hoping your number 1 priority is security, otherwise you may wind up getting burned.
    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
    •