...

View Full Version : Stupid PHP write to file!



martynball
01-25-2010, 11:32 PM
I want to pull in data which is contained in a separate file, this data will be added to the textarea which will allow editing.

Once finished editing, I want it to save to the same file and over write the existing stuff...

Error:


Warning: fopen(includes/Content.php) [function.fopen]: failed to open stream: No such file or directory in /home/martynba/public_html/stokeGTA/editor.php on line 25

Warning: fread(): supplied argument is not a valid stream resource in /home/martynba/public_html/stokeGTA/editor.php on line 26

Warning: fclose(): supplied argument is not a valid stream resource in /home/martynba/public_html/stokeGTA/editor.php on line 27


Actual page:


<?php
session_start();
if(isset($_SESSION['session'])){
//user is logged-in, so do nothing
}
else{
//user needs to log in.
header ("location: scripts/php/login.php");
}
//Get other values for identification
//Post topic
$pageTopic = $_GET['page'];
//Username
$username = $_SESSION['session'];
$username = explode(".", $username);
$username = $username[0];
//Date
$date = date('l jS \of F Y h:i:s A');
if ($pageTopic != "") {
}
else {
echo "URl variable not set, please load page using given link, <a href=\"index.php\">Back!</a>";
}
$myFile = "includes/$pageTopic"."Content.php";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 10000000);
fclose($fh);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Editor</title>
<link rel="stylesheet" href="../stokeGTA/css/mainsheet.css"/>
<script type="text/javascript" src="scripts/js/hoverFix.js"></script>
</head>
<body>
<div class="container" style="padding:3em 0em;">
<span class="editorButton">AL</span>
<span class="editorButton">AM</span>
<span class="editorButton">AR</span>
<span class="editorButton">B</span>
<span class="editorButton">I</span>
<span class="editorButton">U</span>
<span class="editorButton">JS</span>
<span class="editorButton">JS</span>
<form name="editPage">
<textarea class="field" cols="120" rows="20"><?php echo "$theData"; ?></textarea><br />
<input type="submit" name="saveFile" value="Save File">
</form>
<?php
echo "";
if (isset($_POST['saveFile'])) {
$fname = "includes/".$pageTopic."Content.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace("oldword", "newword", "$theData");

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
if (!$fwrite) {
echo "Error saving file!";
} else {
echo "Successfully saved file!";
}
}
?>
</div>
</body>
</html>

mlseim
01-25-2010, 11:40 PM
This is what it can't find:

$myFile = "includes/$pageTopic"."Content.php";

So maybe you meant to do this?

$myFile = "includes/".$pageTopic."."Content.php";

Or is $pageTopic a directory name?

$myFile = "includes/".$pageTopic."/Content.php";

martynball
01-26-2010, 12:04 AM
$pageTopic is the URL variable which is used to make something like this:
weaponContent.php / vehicleContent.php

It wouldn't display the content in the textarea if $myFile wasn't working though :S

met
01-26-2010, 12:17 AM
try this. post results



<?php
session_start();
if(isset($_SESSION['session'])){
//user is logged-in, so do nothing
}
else{
//user needs to log in.
header ("location: scripts/php/login.php");
}
//Get other values for identification
//Post topic
$pageTopic = $_GET['page'];

//Username
$username = $_SESSION['session'];
$username = explode(".", $username);
$username = $username[0];
//Date
$date = date('l jS \of F Y h:i:s A');

if(!isset($pageTitle) || $pageTitle=="") {
echo "URl variable not set, please load page using given link, <a href=\"index.php\">Back!</a>";
} else {
$myFile = "includes/$pageTopic"."Content.php";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 10000000);
fclose($fh);
}
//rest of your code
?>


also ensure that your $_GET is actually "page" so $pageTitle is actually getting set. the error messge you posted suggests $pageTitle is NOT set/has no value.

mlseim
01-26-2010, 12:22 AM
The 2 things in red (below) ... that's suspicious ... Topic or Title?


$pageTopic = $_GET['page'];

//Username
$username = $_SESSION['session'];
$username = explode(".", $username);
$username = $username[0];
//Date
$date = date('l jS \of F Y h:i:s A');

if(!isset($pageTitle) || $pageTitle=="") {



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum