Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file update issue 1

Status
Not open for further replies.

pmonett

Programmer
Sep 5, 2002
2,632
FR
I have a PHP page that is tasked with displaying the content of a file and allowing the user to modify said content.

When user clicks on the submit button, the file is overwritten with the new content and the page refreshes. A message displays to the user the new content that has been written.

My problem is that the code goes through, but the file is not actually modified. The submit happens, the modification message is dispayed, but the file contents do not change.

Code:
<html>
<head>
<title>Editer fichier</title>
<link rel="stylesheet" type="text/css" href="./style.css" title="fichier1">
</head>
<body>
<?php
	include("start.php");
	function customError($errno, $errstr){
		
	}
?>
<tr><td colspan="2" class="page_section">
<h1>Liste des fichier html</h1>
<table class="html_section"><tr><td class="file_section">
<?php
//this section gets the available files and makes a bullet list
	$authorized=array("html");
	$rep=opendir("./html");
	$fichiers[0]="";
    while($entree = readdir($rep)){
		$extention=substr($entree,strlen($entree)-4);
		if(in_array($extention,$authorized)){
			echo "<li><a href='edit.php?&file=$entree'>$entree</a></li>";
		}
    }
	$notify=false;
?>
<br/>
</td><td class="html_section">
<?php
//this section checks for whether or not there is a modification to write and which file to display
	$param_file="";
	set_error_handler("customError");
	$param_file=$_GET["file"];
	$letexte=isset($_REQUEST["modification"])?$_REQUEST["modification"]:".";
//write update if it exists
	if(strlen($letexte)>2){
		$modfic = fopen($filename,"w");
		fputs($modfic,$letexte);
		fclose($modfic);
		$notify=true;
	}
//read file content
	if(strlen($param_file)>0){
		echo "<h2>$param_file</h2>";
		$filename="html/".$param_file;
		$input_file=file($filename);
		echo "<form><input type='hidden' name='file' value='$param_file'/><textarea cols='65' rows='5' name='modification'>";
		foreach($input_file as $k){
			echo $k;
		}
		echo "</textarea><input type='submit' name='$param_file' value='Envoyer' /></form>";
//if there has been a change, notify user
		if($notify){
			echo "<div class='message'> Le texte <br> $letexte <br> a été écrit dans le fichier $fichier </div>";
			$notify=false;
		}
	}
?>
</td></tr></table>
<br/>
</td></tr>
<tr><td colspan="2" class="large_section"><br/><br/></td></tr>
</table>
</body>
</html>

Any ideas ?

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Hi

pmonett said:
$modfic = fopen($filename,"w");
Maybe I am missing something, but where is $filename set ? I mean, where it is set before writing the file ?

Additionally I would check if the web server's user has write permission in the html/ directory and for the given file.

Feherke.
 
Spot on !

With some additional debugging, I found and corrected that problem and one other :

Code:
<?php
//this section checks for whether or not there is a modification to write and which file to display
	$param_file="";
	//$prev_err_handler=set_error_handler("customError");
	//$param_file=$_GET["file"];
	$param_file=isset($_REQUEST["file"])?$_REQUEST["file"]:".";
	$filename="html/".$param_file;
	$letexte=isset($_REQUEST["modification"])?$_REQUEST["modification"]:".";
	if(strlen($letexte)>2){
		$modfic = fopen($filename,"w");
		fputs($modfic,$letexte);
		fclose($modfic);
		$notify=true;
	}
	if(strlen($param_file)>2){
		echo "<h2>$param_file</h2>";
		$input_file=file($filename);
		echo "<form><input type='hidden' name='file' value='$param_file'/><textarea cols='65' rows='5' name='modification'>";
		foreach($input_file as $k){
			echo $k;
		}
		echo "</textarea><input type='submit' name='$param_file' value='Envoyer' /></form>";
//if there has been a change, notify user
		if($notify){
			echo "<div class='message'> Le texte <br> $letexte <br> a été écrit dans le fichier $param_file </div>";
			$notify=false;
		}
	}
?>

Thanks for your time !

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Hi

One minor observation. Instead of
Code:
[navy]$input_file[/navy][teal]=[/teal][COLOR=darkgoldenrod]file[/color][teal]([/teal][navy]$filename[/navy][teal]);[/teal]
[gray]// (...)[/gray]
[b]foreach[/b][teal]([/teal][navy]$input_file[/navy] [b]as[/b] [navy]$k[/navy][teal])[/teal][teal]{[/teal]
    [b]echo[/b] [navy]$k[/navy][teal];[/teal]
[teal]}[/teal]
I would do
Code:
[COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$filename[/navy][teal]);[/teal]


Feherke.
 
Thanks for the tip.

It does seem to do the same thing.

So, is it just for convenience, or is there a better reason ?

Pascal.

I've got nothing to hide, and I'd very much like to keep that away from prying eyes.
 
Hi

Pascal said:
So, is it just for convenience, or is there a better reason ?
Never measured their running time, but I bet [tt]readfile()[/tt] is faster :
[ul]
[li]not splits the file into lines[/li]
[li]not stores the data in the memory[/li]
[/ul]


Feherke.
 
and is written in C and doesn't get called loads of times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top