Forum Moderators: coopster

Message Too Old, No Replies

Problems opening a file

Hmmm....

         

ahmedtheking

8:20 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I think ive done everything right...

// 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>&nbsp;</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>&nbsp;</td>
</tr>
</table>
</form>');

// close file
fclose ($openfile);

Can anyone spot the error or is it a permissions problem?

jatar_k

8:34 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Can anyone spot the error or is it a permissions problem?

what's the error message?

I doubt you can print_r $openfile since print_r expects an array and $openfile is a file pointer

ahmedtheking

10:50 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I tried to print_r $openfile i get:

Resource id #1

Any ideas?

jatar_k

12:23 am on Feb 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



umm, you can't print_r a file pointer, as I mentioned above.

what is the error and what line is it occurring on?

JohnCanyon

10:21 pm on Feb 15, 2005 (gmt 0)

10+ Year Member



$openfile is a file pointer.

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.

jatar_k

11:28 pm on Feb 15, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld JohnCanyon

ahmedtheking

11:33 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the full error:

Warning: file("Resource id #1") - No such file or directory in /home/sites/site64/web/anywhere/admin/normal.php on line 7

ahmedtheking

11:55 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, I keep getting these errors:

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>&nbsp;</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>&nbsp;</td>
</tr>
</table>
</form>');

// close file
fclose ($openfile);

?>

hakre

3:27 pm on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I tried to print_r $openfile i get:
Resource id #1

Any ideas?


other than jatar_k doubts, i'm shure you can use print_r on skalar types (he things print_r needs an array), and its output "Resource id #1" shows that it's a pointer. in this case a file pointer.

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.

jatar_k

5:41 pm on Feb 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> other than jatar_k doubts

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. :)

ahmedtheking

10:47 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes! It works!

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!

jatar_k

5:21 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you look at the contents of the file or just look at the worked var?

On a quick look it may have actually worked though your var might have some scope issues.

hakre

5:28 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



same for me. $worked is only a variable in WriteToFile() but checked in the main, so it is not set there! Try this way:

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)

ahmedtheking

10:49 pm on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sorry my fault again!

Thanks for the help!