...

View Full Version : fopen and fclose help



croatiankid
03-10-2007, 08:20 PM
So I'm using this code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?
$block1 = "block1.txt";
$myblock1 = implode( file( $block1 ), "" );

$block2 = "block2.txt";
$myblock2 = implode( file( $block2 ), "" );

$block3 = "block3.txt";
$myblock3 = implode( file( $block3 ), "" );

$block4 = "block4.txt";
$myblock4 = implode( file( $block4 ), "" );

$block5 = "block5.txt";
$myblock5 = implode( file( $block5 ), "" );

$block6 = "block6.txt";
$myblock6 = implode( file( $block6 ), "" );

$block7 = "block7.txt";
$myblock7 = implode( file( $block7 ), "" );

$block8 = "block8.txt";
$myblock8 = implode( file( $block8 ), "" );


if ( $save1 ) {
$tmpfile1 = fopen( $myblock1, "w" );
$fp = fwrite( $tmpfile1, $block1t );
fclose($tmpfile1);
}
if ( $save2 ) {
$tmpfile2 = fopen( $myblock2, "w" );
$fp = fwrite( $tmpfile2, $block2t );
fclose($tmpfile2);
}
if ( $save3 ) {
$tmpfile3 = fopen( $myblock3, "w" );
$fp = fwrite( $tmpfile3, $block3t );
fclose($tmpfile3);
}
if ( $save4 ) {
$tmpfile4 = fopen( $myblock4, "w" );
$fp = fwrite( $tmpfile4, $block4t );
fclose($tmpfile4);
}
if ( $save5 ) {
$tmpfile5 = fopen( $myblock5, "w" );
$fp = fwrite( $tmpfile5, $block5t );
fclose($tmpfile5);
}
if ( $save6 ) {
$tmpfile6 = fopen( $myblock6, "w" );
$fp = fwrite( $tmpfile6, $block6t );
fclose($tmpfile6);
}
if ( $save7 ) {
$tmpfile7 = fopen( $myblock7, "w" );
$fp = fwrite( $tmpfile7, $block7t );
fclose($tmpfile7);
}
if ( $save8 ) {
$tmpfile8 = fopen( $myblock8, "w" );
$fp = fwrite( $tmpfile8, $block8t );
fclose($tmpfile8);
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>update</title>
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas"
});
</script>
</head>
<body>
<p>index</p>
<form action="update.php">
<p><textarea name="block1t" rows=35 cols=70><? echo $myblock1 ?></textarea></p>
<br>
<p><input type="submit" name="save1" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block2t" rows=35 cols=70><? echo $myblock2 ?></textarea></p>
<br>
<p><input type="submit" name="save2" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block3t" rows=35 cols=70><? echo $myblock3 ?></textarea></p>
<br>
<p><input type="submit" name="save3" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block4t" rows=35 cols=70><? echo $myblock4 ?></textarea></p>
<br>
<p><input type="submit" name="save4" value="save"></p>
</form>
<p>construction</p>
<form action="update.php">
<p><textarea name="block5t" rows=35 cols=70><? echo $myblock5 ?></textarea></p>
<br>
<p><input type="submit" name="save5" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block6t" rows=35 cols=70><? echo $myblock6 ?></textarea></p>
<br>
<p><input type="submit" name="save6" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block7t" rows=35 cols=70><? echo $myblock7 ?></textarea></p>
<br>
<p><input type="submit" name="save7" value="save"></p>
</form>
<form action="update.php">
<p><textarea name="block8t" rows=35 cols=70><? echo $myblock8 ?></textarea></p>
<br>
<p><input type="submit" name="save8" value="save"></p>
</form>


</body>
</html>

invalid xhtml aside, I get
Warning: fopen(<h3>Welcome to our website!</h3> <p>Welcome to our website where we are committed to serving you with all your eyecare needs. We have designed this website with YOU in mind.</p> <p>There are many areas of the site that you can explore. For example, under the health page, you can find out great information about keeping your eyes healthy and avoiding most problems. You can also use the contact form to speak directly with a doctor.</p>) [function.fopen]: failed to open stream: No such file or directory in update.php on line 30

Warning: fwrite(): supplied argument is not a valid stream resource in update.php on line 31

Warning: fclose(): supplied argument is not a valid stream resource in update.php on line 32
when i edit and submit the first field. Also my tinymce isn't working, but i want to fix the php thing first.
line 30 is
$tmpfile1 = fopen( $myblock1, "w" );

Fou-Lu
03-10-2007, 09:34 PM
Ok, well the error is comming because you are not specifying a file to use:

$tmpfile1 = fopen( $myblock1, "w" );
$myblock1 at this point is whatever this:
implode( file( $block1 ), "" );
results in. So you are alloting a string to a file name which it is not capable of handling due to special characters and length restrictions. Simply create an appropriate temporary file name to use.

As well, you can use an array for your html form entries and access the values using a switch. Your submit names would be like save[1], save[2]... save[n] for example. Then you select the file to use through that:


switch ($_REQUEST['save'])
{
case 1:
$useFile = 'tmpFileName2.ext';
break;
case 2:
$useFile = 'tmpFileName2.ext';
break;
...
}

$tmpfile = fopen( $useFile, "w" );
$fp = fwrite( $tmpfile, $block5t );
fclose($tmpfile);

For example.

But as with your actual problem, you just need to create an appropriate resource, namely the file name.

croatiankid
03-10-2007, 09:52 PM
so what i need to actually create physically a temp file? or does the php do it? do i need to put a METHOD in my forms? i will try your method with switch in the morning (it's late here now), but where did you get $block5t for your example, that is, why?

Anthony2oo4
03-10-2007, 10:08 PM
I think it should be:


if ( $save1 ) {
$tmpfile1 = fopen( $tmpfile1, "w" );
$fp = fwrite( $tmpfile1, $block1t );
fclose($tmpfile1);
}

croatiankid
03-11-2007, 09:07 AM
I think it should be:


if ( $save1 ) {
$tmpfile1 = fopen( $tmpfile1, "w" );
$fp = fwrite( $tmpfile1, $block1t );
fclose($tmpfile1);
}
hmm no luck, then I get
Warning: fwrite(): supplied argument is not a valid stream resource in update.php on line 32

Warning: fclose(): supplied argument is not a valid stream resource in update.php on line 33

Fou-Lu
03-11-2007, 09:49 AM
Ah, sorry I'm beat your post kinda blew me away, lol

You can use a variable for a file name is you would like, but you need to state what it is in a string based format (see my switch example to see that). You don't technically need a method to perform this if you do not want to create it, switch() is a built in operator, just think of it as a nice and short if...else if... else statement.

As for creating a temporary file, php (normally) attempt to create it for you, as long as you specify it. It will also try to create the file on a relative directory if I'm not mistaken, so it will create it in the same directory that the executing script is being run in. You should check before running any reads/writes to simply ensure that the file has opened:


if ($handle = fopen('filetoopen.ext', 'w+'))
{
// what to do when the file has opened
}


Quick layout as well for an array returned html form:


<input type="submit" name="save[1]" value="Save" />
<input type="submit" name="save[2]" value="Save" />
<input type="submit" name="save[3]" value="Save" />
<input type="submit" name="save[4]" value="Save" />
<input type="submit" name="save[5]" value="Save" />
<input type="submit" name="save[6]" value="Save" />

Now granted these are on submit boxes so you may only have 1 value return, this is more handy in regards to other form elements. Now, your $_GET/$_POST/$_REQUEST superglobal contains an array named "save" containing the index specified onclick. This is especially handy on radio and check boxes.

Does this answer your questions ok?

[edit]
Oh, just noticed as well that you are using xhtml strict dtd on your html declaration. You need to ensure that your one sided tags are closed, with the ending slash as posted above for the submit inputs. Same goes for your hard breaks if there in there

croatiankid
03-11-2007, 04:58 PM
is a temp file even necessary? all i want to do is edit 8 different text files with 8 different text areas on 1 page.

Fou-Lu
03-11-2007, 08:33 PM
No, it is not required you can directly access the original file, it is your choice. If that is the case, you could technically get away with using only one save as well so you can edit all your text boxes at the same time and save them all. For this, you may need to disable your timelimit depending in the size of the files.

croatiankid
03-12-2007, 08:08 AM
timelimit? okay now I'm confused. I got this script off the internet and edited it (by adding 7 more text areas and the according php at the top). Could you please just write me the code for 1) getting a file's content in a text area (do I even need implode and echo for this, could i not use include()?), 2) saving over the file with the text area (in other words, code to edit a txt file). I could then edit it to add 7 more textareas and saves (I don't mind having a save for every one, just whichever's simpler.), and edit the php code so it all works on 1 page (update.php)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum