0

I am trying to make some statistics in a text document.

I need to have a new unique text document for each item, so therefor I have not made any text documents for now.

But I got this function which I call with the unique ID for the item.

function statestics($id) {
    $filename = 'cache/statestics/opskrift-'.$id.'.txt';

    if(!file_exists($filename)) {
       $fp = fopen($filename,"w");
       fwrite($fp,"0");
       fclose($fp);
    }

    $date = date('d-m-Y');
    $date_exist = 0;
    $todays_num = 0;
    $check1 = explode(',',file_get_contents($filename));
    $content = file_get_contents($filename);
    foreach ($check1 AS $c) {
        $check2 = explode(':',$c);
        if ($check2[0] == $date) {
            $date_exist = 1;
            $todays_num = $check[1];
        }
    }
    if ($date_exist) {
        $insert = $date . ':' . $todays_num . ',';
        $content = str_replace($insert, "", $content);
    }
    $todays_num + 1;
    $content = $content . $insert;
    file_put_contents($filename, $content);
}

But when I run this, it doesn't actually create a single text documents. Any ideas? No errors are reported.

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Jackie Honson
  • 1,419
  • 3
  • 13
  • 12
  • are you working on windows? – bpoiss Feb 05 '13 at 19:46
  • Check the return value of `fopen()`. – Barmar Feb 05 '13 at 19:46
  • `$todays_num + 1;` doesn't do anything, I think you mean `$todays_num++;` – Barmar Feb 05 '13 at 19:47
  • Why are you reading the same file you created at the top of the function? – Barmar Feb 05 '13 at 19:48
  • @BernhardPoiss, I on Linux... - @Barmar, when echo'ing `fopen($filename,"a+");`, it is just empty, but if I echo `file_get_contents($filename);` then I get the real content of the txt document. @Barmar I have now changed to `$todays_num++;`. @Barmar, I first create it, because later i will need it be exist, to change the value. – Jackie Honson Feb 05 '13 at 20:55

1 Answers1

1

To concat string value on end of file should do this:

$fp = fopen($filename,"a+");

If the file exist he just go to end of the file, else he create it.

To use file_get_content and file_put_content, take a look in: here and here

GL

Guerra
  • 2,792
  • 1
  • 22
  • 32
  • He only opens the file if it doesn't already exist, so why would he need to append to the end? – Barmar Feb 05 '13 at 20:12
  • Thanks a lot, this helped with making it create the file, but the file just contains `0` all the time. – Jackie Honson Feb 05 '13 at 20:47
  • @Barmar the param "a+" means: if the file exits open the point to the end of the file(write cases) if not exits the file, he create and open the file to write. – Guerra Feb 05 '13 at 21:00
  • @JackieHonson the file contains0 because you just write 0 in it. lol, to write on file use fwrite($file, "things to write or any string"); look my anwser to more info. – Guerra Feb 05 '13 at 21:03
  • But he has `if (!file_exists($filename))`, so it won't open the file if it already exists. – Barmar Feb 05 '13 at 21:03
  • i know, but this param works any case existing and doesn't existing. just for "learn" the possibilities – Guerra Feb 05 '13 at 21:06