...

View Full Version : Editing PHP File



jabbic
09-12-2005, 08:31 AM
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>";
?>

marek_mar
09-12-2005, 04:27 PM
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

print file_get_contents($file);
is faster.

jabbic
09-13-2005, 08:49 AM
Sorta like this <bits inside this tag gets edited> blah blah blah! </end>

jabbic
09-13-2005, 05:07 PM
does anyone undertsand?

Fou-Lu
09-13-2005, 05:21 PM
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.

jabbic
09-13-2005, 05:27 PM
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. ''; ?>

Fou-Lu
09-13-2005, 05:55 PM
Php you'll need to make use of mysql (http://ca3.php.net/manual/en/ref.mysql.php) 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 // 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($fp, stripslashes($_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?

jabbic
09-13-2005, 06:24 PM
i need to explain what i'm trying to do

jabbic
09-13-2005, 06:30 PM
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.");

?>

Fou-Lu
09-13-2005, 08:29 PM
:eek:
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.

jabbic
09-13-2005, 08:37 PM
can you or someone tell me how to do it?

jabbic
09-14-2005, 05:22 PM
if i cannot do it that way. Could i make a form to submit txt or coding to the bits that say 888

Fou-Lu
09-15-2005, 10:35 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum