dqmg80654 2013-12-03 16:24
浏览 194
已采纳

我尝试fwrite到文本文件时出错

I am trying to make a unique hit counter. I "borrowed" some code from the web but just could not get it to work. (BTW I'm a self taught newbie hack.) Even with the help from the folks here I could NOT get it to work. So I started from scratch writing my own code re-learning the 'f' statements. I now have it so that it will read all of the ip addresses in the text file and compare each one to the users ip, and get out of the loop if it has already been entered.

Now I'm trying to open the count file, read the one entry, increment it by one and write it back to the file. But it just won't work. I found a couple more posts on here and tried what they suggested like the 'cmod' command and all of the possible numbers, but nothing. Then I found how to turn on better error reporting. (Something suggested before but I didn't know how to do it.) Now I get a bunch of errors and I 'think' there is something on the server that's not letting me write to the files. Here's the code I'm using, just for the increment part:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

//  read contents of count.txt

$handle = fopen($count_file, "r");
$old_count=fgets($handle);

echo "Old count = " . $old_count . "<br><br>"; 

fclose($handle);

//  write contents of count.txt

chmod($count_file, 0777);
$fp = fopen($count_file, 'ab');
if (false === $fp) {
    throw new RuntimeException('Unable to open log file for writing');
}

$handle = fopen($count_file, "w");  
$new_count = $old_count +1;

echo "New count = " . $new_count;

fwrite($handle, $new_count);
fclose($handle);

The old count and new count display properly but the new one can't write to the txt file. Here are the error messages that now display:

Warning: chmod() [function.chmod]: Operation not permitted in /home/users/tecitout/counter/fullarray1.php on line 50

Warning: fopen(count.txt) [function.fopen]: failed to open stream: Permission denied in /home/users/tecitout/counter/fullarray1.php on line 51

Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to open log file for writing' in /home/users/tecitout/counter/fullarray1.php:53 Stack trace: #0 {main} thrown in /home/users/tecitout/counter/fullarray1.php on line 53

I really don't understand the errors. Is this an issue with my buddy's server or am I that bad at writing code. I do appreciate the help.

  • 写回答

3条回答 默认 最新

  • drot98385 2013-12-03 16:40
    关注

    If you want to write to a file, you need to have write permissions on it. Also, to chmod a file, you need to either own it, or be logged in as root.

    So your error messages, in order:

    Warning: chmod() [function.chmod]: Operation not permitted in /home/users/tecitout/counter/fullarray1.php on line 50
    

    corresponds to chmod($count_file, 0777);. It would seem that the user that php is running under does not own the file.

    Warning: fopen(count.txt) [function.fopen]: failed to open stream: Permission denied in /home/users/tecitout/counter/fullarray1.php on line 51
    

    This corresponds to the $fp = fopen($count_file, 'ab'); and means that the file is not writeable, because the chmod did not work.

    Fatal error: Uncaught exception 'RuntimeException' with message 'Unable to open log file for writing' in /home/users/tecitout/counter/fullarray1.php:53 Stack trace: #0 {main} thrown in /home/users/tecitout/counter/fullarray1.php on line 53
    

    is because of the if (false === $fp) and is expected, given the above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题