...

View Full Version : Error - fopen, fwrite, fclose



jack020
04-07-2011, 10:42 AM
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
$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
$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. )

oesxyl
04-07-2011, 12:11 PM
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

bullant
04-07-2011, 12:19 PM
check your php.ini on the server to see if allow_url_fopen is set to allow you to use fopen.

oesxyl
04-07-2011, 12:30 PM
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

jack020
04-07-2011, 03:19 PM
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 ? :)


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

oesxyl
04-07-2011, 03:31 PM
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



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 '/'.
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

Fou-Lu
04-07-2011, 03:39 PM
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:


$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:


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);
}

oesxyl
04-07-2011, 03:50 PM
all the files have the right permissions and i just dont have a clue what i have to do.
)


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

Fou-Lu
04-07-2011, 03:53 PM
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.

oesxyl
04-07-2011, 03:58 PM
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

Fou-Lu
04-07-2011, 03:59 PM
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.

oesxyl
04-07-2011, 04:04 PM
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

Fou-Lu
04-07-2011, 04:52 PM
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.

oesxyl
04-07-2011, 05:23 PM
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

jack020
04-08-2011, 07:42 AM
That should be handled gracefully:


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


The op can test this with an if (is_readable($file)) to see if the file is readable or not.

test_readable.php.


<?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
phpinfo();
?>


results.
http://www.2shared.com/document/RRffQ5EA/php_results.html

if it matters at all, just wanted to upload it.

jack020
04-08-2011, 08:51 AM
<?php
$filename = 'Form_Archive.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
?>

result: The file is not writable

so the file is readable but not writable.. why do i get the vague suspicion
that this has nothing to do with my coding :confused:

jack020
04-08-2011, 09:03 AM
yea there was nothing wrong with my coding from the beginning

there server didn't allowed writing in general *facepalm*

ty all for your replies.

Fou-Lu
04-08-2011, 04:24 PM
yea there was nothing wrong with my coding from the beginning

there server didn't allowed writing in general *facepalm*

ty all for your replies.

Yes, it was a privilege issue.
Though you did biff it somewhere here to get this error: PHP Notice: Undefined variable: file in "Website Url"\www\GoMotor\store_it.php on line 28. Given that it was lower in the output errors, that indicates you are trying to use the $file variable beyond the scope of the branch.

The code was a problem nonetheless, you need to properly control your resources to ensure they exist prior to trying to use them and fail gracefully instead. Since writes are non-fatal, failing to do so can make a script appear to work fine, when it has actually failed to write the data.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum