在php上写文件 [英] Write file on php

查看:107
本文介绍了在php上写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留访问者的ips并将它们放在一个文件上。

我尝试了fwrite()函数,但我认为它是在文件上的previus ip重写。

I want to keep ips from visitors and place them on a file.
I tried fwrite() function but I think it is rewrite on the previus ip on file.

示例。

ip.txt为空。

当我运行时write.php脚本,在ip.txt上我有xxxx ip(我的ip)

when I run the write.php Script, on ip.txt I have x.x.x.x ip (my ip)

如果我的朋友运行write.php脚本,在ip.txt上我有aaaa ip (仅限朋友的IP)

If My friend runs the write.php Script, on ip.txt I have a.a.a.a ip (friend's ip only)

我的IP在哪里?
我想在ip.txt文件中拥有以下内容:

where is my ip? I want to have on ip.txt file the following:

x.x.x.x   ip1  
a.a.a.a   ip2

write.php上的代码如下。

Code on write.php is the following.

<?php
$file = fopen("ip.txt","w");
$ip=$_SERVER['REMOTE_ADDR'];
echo fwrite($file,$ip);
fclose($file);
?> 


推荐答案

<?php
$file = fopen("ip.txt","a");
$ip=$_SERVER['REMOTE_ADDR'];
echo fwrite($file,$ip);
fclose($file);
?> 

看看手册

检查第二个参数的含义。

Check what the 2nd parameter means.

您已选择 w 模式,这是一种覆盖模式。尝试 a 模式(追加)

Youve chosen w mode which is an overrwrite mode. Try a mode instead (append)

这篇关于在php上写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆