程序如下:<?php
header('Content-Type:text/html;charset=utf-8');function counter() {
$dir  = 'errorLog';
$file = 'counter.log';

// 新建文件夹
if (! file_exists($dir)) {
mkdir($dir, 0777);
}
if (!is_readable($dir) || !is_writable($dir)) {
chmod($dir, 0777);
}

$num = 1;
if (! file_exists($dir.'/'.$file)) {
$handle = fopen($dir.'/'.$file, 'w');
} else {
if (!is_readable($dir.'/'.$file) || !is_writable($dir.'/'.$file)) {
chmod($dir.'/'.$file, 0777);
}
$handle = fopen($dir.'/'.$file, 'rw');
++$num;
}
fwrite($handle, $num);
fclose($handle);
return $num;
}
?><html>
<head>
</head>
<body>
你好,你是第<?php echo counter(); ?>次浏览该网页!
</body>
</html>输出:
 你好,你是第
Warning: chmod(): Operation not permitted in /usr/local/apache/htdocs/mytest/test2.php on line 21
2次浏览该网页! 第一个异常:Warning,为什么会有警告?我用的是centos 5.6
第二个异常:为什么永远都只系2次?不是应该每刷新一次都增加1吗?

解决方案 »

  1.   

    现在我把程序改成这样了但就是为什么写不入文件?只能显示到2就不能再增加了,文件中永远都只是1
    <?php
    header('Content-Type:text/html;charset=utf-8');function counter() {
    $dir  = 'errorLog';
    $file = 'counter.log';

    // 新建文件夹
    if (! file_exists($dir)) {
    mkdir($dir, 0777);
    } else {
    //echo 'dd';
    }
    if (!is_readable($dir) || !is_writable($dir)) {
    chmod($dir, 0777);
    } else {
    //echo 'ee';
    }

    if (! file_exists($dir.'/'.$file)) {
    //echo 'aa';
    $handle = fopen($dir.'/'.$file, 'w');
    $num = 1;
    } else {
    /*if (!is_readable($dir.'/'.$file) || !is_writable($dir.'/'.$file)) {
    chmod($dir.'/'.$file, 0777);
    //echo 'sadfds@#@';
    } else {
    //echo '342';
    }*/
    $handle = fopen($dir.'/'.$file, 'rw');
    $num = fgetc($handle);
    ++$num;
    echo fwrite($handle, $num);
    }

    fclose($handle);
    return $num;
    }
    ?><html>
    <head>
    </head>
    <body>
    你好,你是第<?php echo counter(); ?>次浏览该网页!
    </body>
    </html>
      

  2.   

    权限不够执行chmod提出两个问题给楼主思考:
    1:为什么需要去检查文件是否存在、判断文件的写权限?这些文件都是你自己在服务器上自己建好了的,你在php里每次执行都要跑去检查一遍是否多此一举,浪费服务器资源?你以为你在写开源框架呀。2:每次执行的时候都是$num=1开始,是否逻辑上有错误?这样执行完应该是每次都显示2咯。那你算出来的值写入counter.log有什么意义呢?反正你下一次执行的时候又不从counter.log里读取数据的,你直接赋值为1了。逻辑思路婚礼哈哈。你思考下,我瞎叨叨几句,不要在意。欢迎楼下各大神拍砖。
      

  3.   

    哦也是哦不过我第二个程序已经改良了。。应该可以写入的。。为什么就不能写入呢??还有您刚才说的不够权限执行chmod,那应该怎么样才能有权限执行呢??
    呃。。还有一个,我这样每次都去检测是否目录存在是为了怕以后某个人(包括自己)不小心把目录删了,那不就更加写入不了了吗?!不过这样子考虑会不会太多虑了。。
      

  4.   

    function counter() {
        $dir  = 'errorLog';
        $file = 'counter.log';
         
        // 新建文件夹
        if (! file_exists($dir)) {
            mkdir($dir, 0666);
            chmod($dir, 0666);
        }
        // 新建文件 
        if (! file_exists($dir.'/'.$file)) {
            $handle = fopen($dir.'/'.$file, 'w');
            fputs($handle, 0);
            fclose($handle);
        }
        //正常流程
        $handle = fopen($dir.'/'.$file, 'r');
        $num = fgets($handle);
        ++$num;
        $handle = fopen($dir.'/'.$file, 'w');
        fputs($handle, $num);
        fclose($handle);
        return $num;
    }
      

  5.   

    多谢你,你的程序是OK的但为什么我的这个又不行呢??function counter() {
    $dir  = 'errorLog';
    $file = 'counter.log';

    // 新建文件夹
    if (! file_exists($dir)) {
    mkdir($dir, 0777);
    }
    if (!is_readable($dir) || !is_writable($dir)) {
    chmod($dir, 0777);
    }

    if (! file_exists($dir.'/'.$file)) {
    $handle = fopen($dir.'/'.$file, 'w');
    $num = 1;
    } else {
    $handle = fopen($dir.'/'.$file, 'rw');
    $num = fgets($handle);
    ++$num;
    }
    fputs($handle, $num);
    fclose($handle);
    return $num;
    和你的差不了多少啊
      

  6.   

    多谢你,你的程序是OK的但为什么我的这个又不行呢??function counter() {
    $dir  = 'errorLog';
    $file = 'counter.log';

    // 新建文件夹
    if (! file_exists($dir)) {
    mkdir($dir, 0777);
    }
    if (!is_readable($dir) || !is_writable($dir)) {
    chmod($dir, 0777);
    }

    if (! file_exists($dir.'/'.$file)) {
    $handle = fopen($dir.'/'.$file, 'w');
    $num = 1;
    } else {
    $handle = fopen($dir.'/'.$file, 'rw');
    $num = fgets($handle);
    ++$num;
    }
    fputs($handle, $num);
    fclose($handle);
    return $num;
    和你的差不了多少啊
    现在主要是写入永远都是1,导致显示增加到2就停下来了
      

  7.   

    差不多是不行的!
    function counter() {
        $dir  = 'errorLog';
        $file = 'counter.log';
         
        // 新建文件夹
        if (! file_exists($dir)) {
            mkdir($dir, 0777); //出于安全考虑,一般不宜同时具有“写”和“执行”权限
        }
        if (!is_readable($dir) || !is_writable($dir)) {
            chmod($dir, 0777); //出于安全考虑,一般不宜同时具有“写”和“执行”权限
        }
         
        if (! file_exists($dir.'/'.$file)) {
            $handle = fopen($dir.'/'.$file, 'w');
            $num = 1;
        } else {
            $handle = fopen($dir.'/'.$file, 'rw'); //虽然是以“读写”方式打开,但打开文件时文件已被清空
            $num = fgets($handle); //所以读到的是空字符
            ++$num; //加 1 后仍然是 1
        }
        fputs($handle, $num);
        fclose($handle);
        return $num;
    }
      

  8.   

    function counter() {
    $dir  = 'errorLog';
    $file = 'counter.log';
     
    // 新建文件夹
    if (! file_exists($dir)) {
    mkdir($dir, 0777);
    }
    if (!is_readable($dir) || !is_writable($dir)) {
    chmod($dir, 0777);
    }
     
    if (! file_exists($dir.'/'.$file)) {
    $handle = fopen($dir.'/'.$file, 'w');
    $num = 1;
    fputs($handle, $num);
    fclose($handle);
    } else {
    $num = intval(file_get_contents($dir.'/'.$file));
    ++$num;
    file_put_contents($dir.'/'.$file, $num);
    }
    return $num;
    }
    可以试试这个代码。