Forum Moderators: coopster
// open the file..
$filename = "/home/sites/site64/web/pages/"."$iden".".php";
$openfile = fopen($filename, "r");
$file = file($openfile);
print_r("$openfile");
exit();
print ('<h1>Updating '.$iden.':</h1><form name="updateform" id="updateform" method="post" action="handle.php?type=update">
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>The Body:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<textarea name="body" id="body" style="height:300px; width:100%; ">
');
echo $file;
print ('
</textarea>
</td>
</tr>
<tr>
<td><input name="id" type="hidden" id="id" value="'.$iden.'" />
<input name="submitbuttom" type="submit" value="Save" />
</td>
<td> </td>
</tr>
</table>
</form>');
// close file
fclose ($openfile);
Can anyone spot the error or is it a permissions problem?
file() loads contents of a file into an array, thus the correct usage based on your code would be....
print_r($file) NOT print_r($openfile)
also since $file is now an array, you cannot just echo $file.. you will need to run through the elements of the array first..
example]
foreach ($file as $line_num => $line) {
$contents .= $line;
}
echo $contents
hope this helps.
Warning: file("Resource id #1") - No such file or directory in /home/sites/site64/web/anywhere/admin/normal.php on line 7
Warning: Invalid argument supplied for foreach() in /home/sites/site64/web/anywhere/admin/normal.php on line 8
Here is the code:
<?php
// open the file..
$filename = "/home/sites/site64/web/pages/".$iden.".php";
$openfile = fopen($filename, "r+");
$file = file($openfile);
foreach ($file as $line_num => $line) {
$contents .= $line;
}
print ('<h1>Updating '.$iden.':</h1><form name="updateform" id="updateform" method="post" action="handle.php?type=update">
<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>The Body:</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<textarea name="body" id="body" style="height:300px; width:100%; ">
');
echo $contents;
print ('
</textarea>
</td>
</tr>
<tr>
<td><input name="id" type="hidden" id="id" value="'.$iden.'" />
<input name="submitbuttom" type="submit" value="Save" />
</td>
<td> </td>
</tr>
</table>
</form>');
// close file
fclose ($openfile);
?>
When I tried to print_r $openfile i get:
Resource id #1Any ideas?
like your error states: file [php.net]("Resource id #1") is trying to open "Resource id #1" as a file, which isn't a file or directory. you really wonder that the file "Resource id #1" does not exists on your harddrive? won't guess so, because you're setting up a filename in line 5:
$filename = "/home/sites/site64/web/pages/".$iden.".php";
so just remove line 6 and correct line 7 to:
$file = file($filename);
these are my ideas, hope they help you :)
this should do the trick then. for output, you can then shortcut with implode() [php.net], so there is no need to code a foreach {} where even the prevaluating of $contents was missing:
$contents = implode("\n",$file); use this as the new line 7 then and remove lines 8-10 then.
hehe, ok, let me qualify that better then, if you use print_r on a variable that is not an array or object it will just echo the value, which I did not think was the desired functionality here. You sure can't get any desired results from using it on a file pointer. ;)
and yes I was in error thinking it expected an array since it expects mixed. :)
Nice one!
Ok, one down, now I gotta write to the file!
Is this any good...
The form where the data is called will be sent to a handle file that consists of:
// trim the body
$trimmedbody = trim($body);function WriteToFile ($iden, $trimmedbody) {
$filename = "/home/sites/site64/web/pages/".$iden.".php"; // finds the file
$openfile = fopen ($filename, "w+"); // open file
// if write works...
if (fwrite ($openfile, $trimmedbody)) {
$worked = "1"; }
fclose ($openfile); // close the file
}
WriteToFile($iden, $trimmedbody);
// begin content
echo '<h1>Updating '.$iden.':</h1>';
// if write worked...
if ($worked == "1") {
echo "<p>You have successfully written the following to file \"".$iden.".php\":</p>";
echo strip_tags($trimmedbody);
} else {
echo "<p>You have <b>FAILED</b> to write the following to file \"".$iden.".php\":</p>";
echo strip_tags($trimmedbody);
echo "<p>Please contact the <a href=\"mailto:ahmed@firestartermedia.co.uk\">Webmaster</a> about this issue (Remember to copy and paste the source code of this page!)</p>";
}
And it doesn't work! Why oh why!
function WriteToFile ($iden, $trimmedbody) {
$filename = "/home/sites/site64/web/pages/".$iden.".php"; // finds the file
$openfile = fopen ($filename, "w+"); // open file
if ($openfile) {
// if write works...
if (fwrite ($openfile, $trimmedbody)) {
fclose ($openfile); // close the file
return 2;
}
fclose ($openfile); // close the file
return 1;
} else {
return 0;
} then use it like that:
$r = WriteToFile(...) $r will then be:
0 - could not open file (ERROR)
1 - could not write to file (ERROR)
2 - could open and write to file (OK)