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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Error - fopen, fwrite, fclose

    hello ppl from CF,

    i have a PHP code that writes information from the textboxes in Index_test.php towards a .txt file in my ./ directory. ( fwrite storage code )

    it worked perfectly on my local webserver but now that i uploaded it to the site i get this error:


    PHP Warning: fopen(./Form_Archive.txt): failed to open stream:
    Permission denied in (http Adress)\www\GoMotor\store_it.php on line 20 PHP Warning: fwrite() expects parameter 1 to be resource,
    boolean given in (http Adress)\www\GoMotor\store_it.php on line 21 PHP Warning: fclose() expects parameter 1 to be resource,
    boolean given in (http Adress)\www\GoMotor\store_it.php on line 22


    all the files have the right permissions and i just dont have a clue what i have to do.


    store_it.php
    PHP Code:
    <?php
    $voor 
    = empty($_POST["voor"]) ? "" $_POST["voor"];
    $achter = empty($_POST["achter"]) ? "" $_POST["achter"];
    $adres = empty($_POST["adres"]) ? "" $_POST["adres"];
    $prov = empty($_POST["prov"]) ? "" $_POST["prov"];
    $stad = empty($_POST["stad"]) ? "" $_POST["stad"];
    $telefoon = empty($_POST["telefoon"]) ? "" $_POST["telefoon"];
    $email = empty($_POST["email"]) ? "" $_POST["email"];
    $check1 = empty($_POST["check1"]) ? "" $_POST["check1"];
    $check2 = empty($_POST["check2"]) ? "" $_POST["check2"];
    $check3 = empty($_POST["check3"]) ? "" $_POST["check3"];

    if(!
    $voor || !$achter || !$adres || !$prov || !$stad || !$telefoon || !$email || !$check2){header("location:index_TEST.php");
    }

    else

    {
     
    $file "Form_Archive.txt";
     
    $a fopen($file"a");
     
    fwrite($a,$voor.";".$achter.";".$adres.";".$prov.";".$stad.";".$telefoon.";".$email.";".$check1.$check3.";".$check2."\r\n");
     
    fclose($a);
     }
    ?>

    <form action="index_TEST.php">

    <?php
    $ref 
    $_SERVER['HTTP_REFERER'];
    header('refresh: 6; url='.$ref);
    ?>

    <style type="text/css">
        #myoutercontainer {  }
        #myinnercontainer { position:absolute; top:50%; left:20%; height:10em; margin-top:-5em }
    </style>


    <div id="myoutercontainer">
        <div id="myinnercontainer">




        <img id="top" src="top.png" alt="">
                <center>
                <h1><a>Bedankt u heeft uw formulier succesvol verstuurd.</a></h1>
                </center>
        <img id="top" src="top.png" alt="">


        </div>
    </div>


    </form>
    index_test.php
    PHP Code:
    <?php
    $voor 
    = empty($_GET["voor"]) ? "" $_GET["voor"];
    $achter = empty($_GET["achter"]) ? "" $_GET["achter"];
    $adres = empty($_GET["adres"]) ? "" $_GET["adres"];
    $prov = empty($_GET["prov"]) ? "" $_GET["prov"];
    $stad = empty($_GET["stad"]) ? "" $_GET["stad"];
    $postcode = empty($_GET["postcode"]) ? "" $_GET["postcode"];
    $telefoon = empty($_GET["telefoon"]) ? "" $_GET["telefoon"];
    $email = empty($_GET["email"]) ? "" $_GET["email"];
    $check1 = empty($_GET["check1"]) ? "" $_GET["check1"];
    $check2 = empty($_GET["check2"]) ? "" $_GET["check2"];
    $check3 = empty($_GET["check3"]) ? "" $_GET["check3"];

    if(!
    $voor){$message1 "";}
    if(!
    $achter){$message1 "";}
    if(!
    $adres){$message3 "";}
    if(!
    $prov){$message1 "";}
    if(!
    $stad){$message2 "";}
    if(!
    $postcode){$message3 "";}
    if(!
    $telefoon){$message1 "";}
    if(!
    $email){$message1 "";}
    if(!
    $check1){$message1 "";}
    if(!
    $check3){$message1 "";}
    ?>


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Form</title>
    <link rel="stylesheet" type="text/css" href="view.css" media="all">
    <script type="text/javascript" src="view.js"></script>


    <script  type="text/javascript">
    /*<![CDATA[*/

    function ButtonDisable1(ip1,ip2,ip3,ip4,ip5,ip6,ip7,ip8,but){
     ip1=document.getElementById(ip1).value;
     ip2=document.getElementById(ip2).value;
     ip3=document.getElementById(ip3).value;
     ip4=document.getElementById(ip4).value;
     ip5=document.getElementById(ip5).value;
     ip6=document.getElementById(ip6).value;
     ip7=document.getElementById(ip7).value;
     var ip8=document.getElementById(ip8);
     but=document.getElementById(but);
     but.setAttribute('disabled','disabled');
     if (ip1.replace(/\s/g).length>0&&ip2.replace(/\s/g).length>0&&ip3.replace(/\s/g).length>0&&ip4.replace(/\s/g).length>0&&ip5.replace(/\s/g).length>0&&ip6.replace(/\s/g).length>0&&ip7.replace(/\s/g).length>0&&ip8.checked){
      but.removeAttribute('disabled');
     }
    }
    ip9.replace(/\s/g).eregi
    /*]]>*/
    </script>

    </head>



    <body id="main_body">






    <caption
        <div id="form_container">
        
            <h1><a>Contact formulier</a></h1>
            <form action="store_it.php" method="post">
                        <div class="form_description">
                <h2>&nbsp;GoMotor deelnameformulier</h2>
                <p>&nbsp;vul de onderstaande gegevens in om kans te maken op...</p>
            </div>
                <ul>
     <table>
      <tr>
       <td class="tdr">&nbsp;<font size="2">Voornaam:</td>
       <td>&nbsp;<input type="text" name="voor" value="<?php echo $voor?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')">
       </td>
      </tr>
      <tr>
       <td class="tdr">&nbsp;<font size="2">Achternaam:</td>
       <td>&nbsp;<input type="text" name="achter" value="<?php echo $achter?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>

      </tr>
      <tr>
       <td class="tdr">&nbsp;<font size="2">Adres:</td>
       <td>&nbsp;<input type="text" name="adres" value="<?php echo $adres?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>
      </tr>
      <tr>
       <td class="tdr">&nbsp;<font size="2">Postcode:</td>
       <td>&nbsp;<input ID="prov" type="text" name="prov" value="<?php echo $prov?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>
         </tr>
      <tr>
       <td class="tdr">&nbsp;<font size="2">Woonplaats:</td>
       <td>&nbsp;<input type="text" name="stad" value="<?php echo $stad?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>
         </tr>
         <tr>
       <td class="tdr">&nbsp;<font size="2">Telefoon:</td>
       <td>&nbsp;<input type="text" name="telefoon" value="<?php echo $telefoon?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>
         <tr>
       <td class="tdr">&nbsp;<font size="2">Email:</td>
       <td>&nbsp;<input type="text" name="email" value="<?php echo $email?>" onKeyUp="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')"></td>
        </tr>
       <td><P>&nbsp;<p></td>
       <tr valign="top" height="50px">
       <td class="tdr">&nbsp;<font size="2">Bent u in het bezit van een geldig motor rijbewijs?</td>
          <td>
                  &nbsp;<SELECT name="check1" width="150">
                  <option SELECTED value="Nee" <?PHP print $check3?>> Nee 
                  <option value="Ja" <?PHP print $check1?>> Ja
                  </select>
       </td>
       </tr>
       <tr valign="top">
       <td><font size="2"><p>&nbsp;Hierbij bevestig ik dat de hierboven ingevulde gegevens correct <br>&nbsp;en naar waarheid zijn ingevuld en vrijgegeven mogen worden <br>&nbsp;voor de deelnemers van het motorcollectief, zijnde: Kawasaki, <br>&nbsp;KTM, Ducati, B.V. Ninmag - Suzuki, Triumph Motorcycles, Putoline Oil, <br>&nbsp;De Watertoren en Harley Davidson. Door het geven van <br>&nbsp;mijn akkoord maak ik kans op de beschikbaar gestelde prijzen <br>&nbsp;door het genoemde motorcollectief.</td> 
       <td valign="top"><input type="checkbox" ID="check2" name="check2" onClick="ButtonDisable1('voor','achter','adres','prov','stad','telefoon','email','check2','submit_button')" value="Akkoord"<?PHP print $check2?>>
       </td>
       </tr>
      <tr>
       <td colspan="1" align="left"><P>&nbsp;<p>&nbsp;<input type="Button" value="Reset Formulier" ONCLICK="window.location.href='index_TEST.php'"> <input type="submit" id="submit_button" value="Verzenden" disabled="true"></td>
         </tr>
                </ul>
            </form>

        </div>

    </caption>
        </body>
    <a href="http://~"><img src="trans2.gif" style= "position: absolute; top: 726px; left: 385px;"/></a>
    </html>
    ( free to use for any1 who would want to btw. or else i wouldn't be uploading it. )

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jack020 View Post
    hello ppl from CF,

    i have a PHP code that writes information from the textboxes in Index_test.php towards a .txt file in my ./ directory. ( fwrite storage code )

    it worked perfectly on my local webserver but now that i uploaded it to the site i get this error:


    PHP Warning: fopen(./Form_Archive.txt): failed to open stream:
    Permission denied in (http Adress)\www\GoMotor\store_it.php on line 20 PHP
    try to avoid relative path, filesystem path and server path are different.
    use $_SERVER['DOCUMENT_ROOT'] instead.

    http://php.net/manual/en/reserved.variables.server.php

    best regards

  • Users who have thanked oesxyl for this post:

    jack020 (04-07-2011)

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    check your php.ini on the server to see if allow_url_fopen is set to allow you to use fopen.

  • Users who have thanked bullant for this post:

    jack020 (04-07-2011)

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by bullant View Post
    check your php.ini on the server to see if allow_url_fopen is set to allow you to use fopen.
    you bet?,

    best regards

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bullant View Post
    check your php.ini on the server to see if allow_url_fopen is set to allow you to use fopen.
    i looked on the server where i needed to upload the form on, they don't have any php files at all

    ( my xampp localserver obviously had )

    could that be the reason ?

    Quote Originally Posted by oesxyl View Post
    try to avoid relative path, filesystem path and server path are different.
    use $_SERVER['DOCUMENT_ROOT'] instead.

    http://php.net/manual/en/reserved.variables.server.php

    best regards
    i used this but it didn't worked:

    ($_SERVER['DOCUMENT_ROOT']."/www/GoMotor/Form_Archive.txt");

    don't know if its right but i think the issue that i adressed in my reply towards bullant has to be solved first for me to be able to test this out any further ^^

    ty both for your replies
    Last edited by jack020; 04-07-2011 at 03:21 PM.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jack020 View Post
    i used this but it didn't worked:

    ($_SERVER['DOCUMENT_ROOT']."/www/GoMotor/Form_Archive.txt");

    don't know if its right but i think the issue that i adressed in my reply towards bullant has to be solved first for me to be able to test this out any further ^^

    ty both for your replies
    PHP Code:
    echo $_SERVER['DOCUMENT_ROOT']."/www/GoMotor/Form_Archive.txt"
    i suspect you need to remove www and to check if $_SERVER['DOCUMENT_ROOT'] ends in '/' and in this case to remove also the first '/'.
    Edit: this is to avoid to have something like '...public_html//www...'

    for short, i guess is something like this:

    $_SERVER['DOCUMENT_ROOT']."/GoMotor/Form_Archive.txt"

    or like this:

    $_SERVER['DOCUMENT_ROOT']."GoMotor/Form_Archive.txt"

    but both are guesses,

    best regards

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is a privilege issue. allow_url_fopen has nothing to do with this; that only applies in the event that the fopen is utilizing the http stream wrappers. Using a relative or absolute filesystem path does not require this directive. I'm a fan of relative pathing myself, so if you choose to do so, use the realpath() function to expand the path to the full filesystem path.

    Look at the error; PHP can find this file with no problem:
    PHP Warning: fopen(./Form_Archive.txt): failed to open stream:
    Permission denied in (http Adress)\www\GoMotor\store_it.php on line 20
    This is the only error that matters. The remaining errors are from incorrect error handling. The error states that permission is denied on Form_archive.txt. The part that *looks* tricky is the (http Address) part, which is where the execution has failed not the location of the file itself.
    The solution is simple, chmod the file to open it for read and write (if necessary) access to the apache user. This is unfortunately named differently for different servers, I've seen Apache, nobody, www-user, and so forth. The *easiest* think to do, is to simply allow PHP to create this original file for you, and copy the original data back into it. PHP will automatically generate the required privileges for the file.

    To fix the remaining errors, its your job to ensure proper error handling:
    PHP Code:
     $a fopen($file"a");
     
    // No, what if the fopen failed?
    fwrite($a,$voor.";".$achter.";".$adres.";".$prov.";".$stad.";".$telefoon.";".$email.";".$check1.$check3.";".$check2."\r\n");
      
    // No what if the fopen failed?
     
    fclose($a); 
    That should be handled gracefully:
    PHP Code:
    if ($fh = @fopen($file'a'))
    {
        if (!@
    fwrite($fh,$voor.";".$achter.";".$adres.";".$prov.";".$stad.";".$telefoon.";".$email.";".$check1.$check3.";".$check2."\r\n"))
        {
            print(
    'Failed to write to file.');
        }
        
    fclose($fh);
    }
    else
    {
        
    printf('Failed to open %s for writing'$file);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by jack020 View Post
    all the files have the right permissions and i just dont have a clue what i have to do.
    )
    Quote Originally Posted by Fou-Lu View Post
    The solution is simple, chmod the file to open it for read and write (if necessary) access to the apache user.
    when a user ask for help and say that files have the right permissions i tend to belive that sometime in the past he/she had troubles with this,

    best regards

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by oesxyl View Post
    when a user ask for help and say that files have the right permissions i tend to belive that sometime in the past he/she had troubles with this,

    best regards
    I would agree; however, the error does say otherwise.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    I would agree; however, the error does say otherwise.
    is a guess, but the error could also say that can't find the file.

    best regards

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by oesxyl View Post
    is a guess, but the error could also say that can't find the file.

    best regards
    Correct, but if the file is not retrievable the error would indicate that the file cannot be found, not permission denied.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    Correct, but if the file is not retrievable the error would indicate that the file cannot be found, not permission denied.
    i have the feeling that fopen message will not complain about find or not, same permision denied( apache drive this car),

    best regards

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by oesxyl View Post
    i have the feeling that fopen message will not complain about find or not, same permision denied( apache drive this car),

    best regards
    Ok, I see what you may mean. If the file does not exist at all, write modes will attempt to created it which may throw the permission denied error, but offhand I cannot be sure.
    If the file does exist and is unreadable, then it will throw a No such file or directory error.
    The op can test this with an if (is_readable($file)) to see if the file is readable or not.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    jack020 (04-08-2011)

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    Ok, I see what you may mean. If the file does not exist at all, write modes will attempt to created it which may throw the permission denied error, but offhand I cannot be sure.
    If the file does exist and is unreadable, then it will throw a No such file or directory error.
    The op can test this with an if (is_readable($file)) to see if the file is readable or not.
    i guess op try to solve the path problem now. I'm not sure too about my statements, so will see when op will provide feedback,

    best regards

  • Users who have thanked oesxyl for this post:

    jack020 (04-08-2011)

  • #15
    New Coder
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That should be handled gracefully:
    PHP Code:
    if ($fh = @fopen($file'a'))
    {
        if (!@
    fwrite($fh,$voor.";".$achter.";".$adres.";".$prov.";".$stad.";".$telefoon.";".$email.";".$check1.$check3.";".$check2."\r\n"))
        {
            print(
    'Failed to write to file.');
        }
        
    fclose($fh);
    }
    else
    {
        
    printf('Failed to open %s for writing'$file); ( line 28. )

    Failed to open for writing

    PHP Notice: Undefined variable: file in "Website Url"\www\GoMotor\store_it.php on line 28

    Quote Originally Posted by Fou-Lu View Post
    The op can test this with an if (is_readable($file)) to see if the file is readable or not.
    test_readable.php.
    PHP Code:
    <?php
    $filename 
    'Form_Archive.txt';
    if (
    is_readable($filename)) {
        echo 
    'The file is readable';
    } else {
        echo 
    'The file is not readable';
    }
    ?>
    result:

    The file is readable

    ;

    checked it again all files are 777. read, write and executable

    test.php
    PHP Code:
    <?php
    phpinfo
    ();
    ?>
    results.
    http://www.2shared.com/document/RRff...p_results.html

    if it matters at all, just wanted to upload it.
    Last edited by jack020; 04-08-2011 at 08:03 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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