1

我想创建一个文件来缓存我的查找(坐标等)。我不知道为什么,但我无法在 WordPress 中创建和写入它。我正在使用此代码进行尝试:

<?php

 $filename = 'sitevisitors.txt';

 if (file_exists($filename)) 
 {
    $count = file(TEMPLATEPATH . 'sitevisitors.txt'); 
    $count[0] ++;
    $fp = fopen(TEMPLATEPATH . "sitevisitors.txt", "w");
    fputs ($fp, "$count[0]");
    fclose ($fp);
    echo $count[0];
 } 

 else 
 {
    $fh = fopen(TEMPLATEPATH . "sitevisitors.txt", "w");
    if($fh==false)
        die("unable to create file");
    fputs ($fh, 1);
    fclose ($fh);
    $count = file(TEMPLATEPATH . 'sitevisitors.txt'); 
    echo $count[0];
 }

 ?> 

我没有收到任何错误消息,但文件“sitevisitors.txt”没有创建和更新,也没有出现在我的服务器上。我究竟做错了什么?路径应该没问题。我的服务器主机确认我拥有完全权限。这段代码在 WordPress 之外运行良好......

欢迎任何建议!

干杯,玛丽娜

4

1 回答 1

2

TEMPLATEPATH 常量末尾没有斜杠,您应该像这样使用它:

$fh = fopen(TEMPLATEPATH . "/sitevisitors.txt", "w");

注意文件名前的斜线

于 2011-09-14T12:14:08.307 回答