/    Sign up×
Community /Pin to ProfileBookmark

Opening a file, then saving it?

I am a PHP newbie, and I have written a script, but I am not sure why it does not work.
The script is supposed to open ‘banip.txt’ where there is a list of banned IP addresses (1 per line). You then change what is in the text area (e.g. add an IP) and then you click Save, then it saves the text file.

This does not work, what seems to happen is the file does got get modified when you post the form.

Could someone please correct my code? This is for running on PHP5 in Windows.

[code=php]
<?
/*
require(“config.php”);
require(“functions.php”);
mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
$auth = auth();
if ($auth != 2) { die(‘Access denied’); }
*/

if($ips != “” ) {
echo $ips; //for debug
fwrite(‘banip.txt’,$ips);
fclose(‘banip.txt’);
}
?>

<form action=”<? $_SERVER[‘PHP_SELF’] ?>” method=”post”><?
$ips = file(‘banip.txt’);
echo”<textarea name=”ips”>”;
$ips = implode(“”, $ips);
echo $ips;
echo”</textarea>”;
fclose($ips);
?><input type=”submit” value=”Save”>
</form>

[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 07.2004 — [code=php]
if(fopen('banip.txt','a+') // added this line to open file
{
fwrite('banip.txt',$ips);
fclose('banip.txt');
}
else
{
echo "<p>Sorry, could not open the file.</p>n";
}
[/code]

Also, you need to ensure that the user under whom the PHP processes are run (it's not you, but a user often called "nobody" or such) has permission to write to this file.
Copy linkTweet thisAlerts:
@darkestnightauthorOct 07.2004 — Thank you NogDog, but your code does not seem to work for me! ?

When I try getting/posting to banip2.php, the page apears completly blank.

banip.php
[code=php]
<?
/*
require("config.php");
require("functions.php");
mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());
$auth = auth();
if ($auth != 2) { die('Access denied'); }
*/

?>

<form action="banip2.php" method="post"><?
$ips = file('banip.txt');
echo"<textarea name="ips">";
$ips = implode("", $ips);
echo $ips;
echo"</textarea>";
fclose($ips);
?><input type="submit" value="Save">
</form>
[/code]


banip2.php
[code=php]
<?
if(fopen('banip.txt','a+') // added this line to open file
{
fwrite('banip.txt',$ips);
fclose('banip.txt');
}
else
{
echo "Could not open the file";
}
?>
[/code]
Copy linkTweet thisAlerts:
@yunaOct 08.2004 — Unlike file() which takes a filename or URL as an argument, the fread() and fwrite() functions use a "resource" pointer that comes from a call to fopen(). It works like this:

[code=php]
# open a file
$fp=fopen("/path/to/your/file","a+");

# write some stuff to it
while (list(,$val)=each($yourarray)) {
fwrite($fp,$val);
}

# close the file
fclose($fp);
[/code]


I'd recommend reading carefully the manual sections on the use of the various file-handling functions. Start with http://www.php.net/fopen.

Writing a file in PHP requires that the username the web server runs as has permissions to write in the directory into which the file will be saved.
Copy linkTweet thisAlerts:
@NogDogOct 08.2004 — Doh! I forgot all about that. That's what happens when I type off the top of my head and don't test it (and work off-and-on with 3 or 4 different scripting languages).
Copy linkTweet thisAlerts:
@darkestnightauthorOct 08.2004 — Thank you for your help, but I do not understand how to intergrate your code snippet into my script ?
Copy linkTweet thisAlerts:
@NogDogOct 08.2004 — [code=php]
<?
if($handle = fopen('banip.txt','a+'))
{
fwrite($handle, $ips);
fclose($handle);
}
else
{
echo "Could not open the file";
}
?>
[/code]
Copy linkTweet thisAlerts:
@darkestnightauthorOct 08.2004 — I've got it working, thanks! ?

Sorry for being a n00b!
Copy linkTweet thisAlerts:
@drythirstOct 09.2004 — Well, everyone has to start, just some people start at different times, especially if one person was born before another (obviously...).
×

Success!

Help @darkestnight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...