Posting to self with a Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Posting to self with a Function

Post by sinewave »

I am trying to write a function where it will accept a filename as a parameter and open it up for editing.

Code: Select all

<?php
function file_edit($filename)
{

	if (isset($HTTP_POST_VARS[Submit])) 
	{
		$non_breaking_spaces = nl2br(str_replace('  ', ' &nbsp;', $HTTP_POST_VARS[text1]));
		$fp = @fopen($filename, "w+");
		if($fp)
		{ 
    		$cont = fwrite($fp, $non_breaking_spaces); 
    		fclose($fp); 
		} 
		echo "Updated Successfully";
		exit();
	}

	$fp = @fopen($filename, "r");

	if($fp)
	{ 
    	$cont = str_replace ("<br />", "",fread($fp, filesize($filename))); 
    	fclose($fp); 
	} 

	echo "<form action="".$PHP_SELF."" method="post" name="TextEdit">\n\t<textarea class="textarea" name="text1" cols="50" rows="25">\n";
 	echo stripslashes($cont);
 	echo"</textarea>\n\t<BR>\n\t<input type="submit" name="Submit" value="Submit">\n</form>\n";


}


?>

It accepts it and reads it fine, but i am unsure how to submit it from another file.

Example)
Another file will have this code:

Code: Select all

<?php
include("file_functions.php"); // This is the file containing above code
file_edit("news.txt");

?>
I think the problem lies in the $PHP_SELF since it wont be itself anymore it is uncluded elsewhere. Anyone have any idea what i could do?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$_SERVER['PHP_SELF'] will be the name of the file that called the include..not the included file name.
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

Right, but what could i do?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not sure what your problem is now as the PHP_SELF thing isn't a problem ;)
The code you posted should work fine (presuming the function does indeed work as you said it does).
i.e doing

Code: Select all

include_once 'file_functions.php';
file_edit('news.txt');
shouldn't be a problem.
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

It doesn't work because

Code: Select all

if (isset($HTTP_POST_VARS[Submit]))
is inside the function, so when it posts to itself it doesnt know what to do.
It also wont work if i put it outside of the function because then it doesnt know the filename.
Any ideas?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, if you're using PHP => 4.1.0 then you should really use

Code: Select all

if(isset($_POST['Submit'])){
...
}
otherwise you'll need to do

Code: Select all

function file_edit($filename){
    global $HTTP_POST_VARS;
    if (isset($HTTP_POST_VARS['Submit']))
....
The posting to itself, $_SERVER['PHP_SELF'] isn't an issue as if file1.php includes file2.php, and file2.php does a POST to $_SERVER['PHP_SELF'] it will POST to file1.php as normal. By the way, your script as it is requires register_globals to be On, and they are Off by default in most recent versions of PHP, so might be worth checking that too as your script won't work otherwise.
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

It shouldn'trequire register_globals at all

$filename is just the parameter.

When it submits, it will never be able to reach that line because that line is WITHIN a function and doesnt get called.

If i put it outside of that function it doesnt know the filename.
I can use a hidden field to remember the filename, maybe i should just try that. That way i should be able to have multiple fields on one page...i think.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ok, here's how i'd do what you're trying to do..(and i've tried and tested this ;))

file1.php

Code: Select all

require_once 'file_functions.php';
file_edit('news.txt');
file_functions.php

Code: Select all

<?php
function file_edit($filename){
    if(isset($_POST['Submit'])){
        $fp = fopen($filename, 'w');
        fputs($fp, $_POST['text1']);
        fclose($fp);
        echo 'Updated Successfully';
    }else{
        $text1 = file_get_contents($filename);
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
            <textarea class="textarea" name="text1" cols="50" rows="25"><?php echo $text1 ?></textarea>
            <input type="submit" name="Submit" value="Submit" />
        </form>
    <?php
    }
}
?>
Last edited by markl999 on Thu Oct 16, 2003 8:43 pm, edited 3 times in total.
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

This is the way i chose to do it. It works fine and you can have multiple files on 1 web page. Notice the saving is outside of the function. It works good. Thought i would post it if you were interested.

It also replaces nl2br and vice versa.

Code: Select all

<?php

if (isset($_POST[SubmitTextEdit])) 
	{
		$filename = $_POST[filename];	
		$non_breaking_spaces = nl2br(str_replace('  ', ' &nbsp;', $_POST[text1]));
		$fp = @fopen($filename, "w+");
		if($fp)
		{ 	$cont = fwrite($fp, $non_breaking_spaces); 
    		fclose($fp); 
		} 
		echo "Updated Successfully";
		exit();
}



function file_edit($filename)
{
	$fp = @fopen($filename, "r");

	if($fp)
	{  	$cont = str_replace ("<br />", "",fread($fp, filesize($filename))); 
    	fclose($fp); 
	} 
	
	echo "<form action="".$PHP_SELF."" method="post" name="TextEdit">\n\t<textarea class="textarea" name="text1" cols="50" rows="25">\n";
 	echo stripslashes($cont);
	echo"</textarea>\n\t<BR>\n\t<input type="hidden" name="filename" value="".$filename."">\n";
 	echo"<BR>\n\t<input type="submit" name="SubmitTextEdit" value="Submit">\n</form>\n";
	
}
?>
Post Reply