Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts

    fopen and fclose help

    So I'm using this code
    PHP 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 implodefile$block1 ), "" );

    $block2 "block2.txt";
    $myblock2 implodefile$block2 ), "" );

    $block3 "block3.txt";
    $myblock3 implodefile$block3 ), "" );

    $block4 "block4.txt";
    $myblock4 implodefile$block4 ), "" );

    $block5 "block5.txt";
    $myblock5 implodefile$block5 ), "" );

    $block6 "block6.txt";
    $myblock6 implodefile$block6 ), "" );

    $block7 "block7.txt";
    $myblock7 implodefile$block7 ), "" );

    $block8 "block8.txt";
    $myblock8 implodefile$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
    PHP Code:
    $tmpfile1 fopen$myblock1"w" ); 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    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?
    Last edited by croatiankid; 03-10-2007 at 10:54 PM.

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    I think it should be:

    PHP Code:
    if ( $save1 ) {
    $tmpfile1 fopen$tmpfile1"w" );
    $fp fwrite$tmpfile1$block1t );
    fclose($tmpfile1);


  • #5
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by Anthony2oo4 View Post
    I think it should be:

    PHP Code:
    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

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    if ($handle fopen('filetoopen.ext''w+'))
    {
      
    // what to do when the file has opened

    Quick layout as well for an array returned html form:
    Code:
    <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
    Last edited by Fou-Lu; 03-11-2007 at 10:54 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #9
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •