2012-11-28 81 views
5

我想在一个文本文件来记录下载增量数

有人来到我的网站,下载的东西,它会添加一个新行到文本文件,如果它不是已经或增加当前一。

我已经试过

$filename = 'a.txt'; 
$lines = file($filename); 
$linea = array(); 

foreach ($lines as $line) 
{ 
    $linea[] = explode("|",$line); 
} 

$linea[0][1] ++; 

$a = $linea[0][0] . "|" . $linea[0][1]; 

file_put_contents($filename, $a); 

但它总是超过1

增加它的文本文件格式是

name|download_count 
+0

“.txt”文件中有多少行......? – NazKazi

+1

发布你的'txt'文件的一些示例内容 –

+0

一个数据库比这个文本文件要容易得多 – 2012-11-28 05:49:38

回答

2

你做你递增for循环外,并且只能访问[0] th元素,所以在其他地方没有任何变化。

这也许应该是这个样子:

$filename = 'a.txt'; 
$lines = file($filename); 

// $k = key, $v = value 
foreach ($lines as $k=>$v) { 
    $exploded = explode("|", $v); 

    // Does this match the site name you're trying to increment? 
    if ($exploded[0] == "some_name_up_to_you") { 
     $exploded[1]++; 

     // To make changes to the source array, 
     // it must be referenced using the key. 
     // (If you just change $v, the source won't be updated.) 
     $lines[$k] = implode("|", $exploded); 
    }   
} 

// Write. 
file_put_contents($filename, $lines); 

你或许应该使用这个数据库,虽然。看看PDO和MYSQL,你会走向迷人的路。


编辑

要你在你的评论中提到的,你可以设置一个布尔标志,并触发它,你走过的阵列。这可以保证一个break也一样,如果你只是在寻找一两件事:它存储在后您使用的是foreach循环

... 
$found = false; 
foreach ($lines as $k=>$v) { 
    $exploded = explode("|", $v); 

    if ($exploded[0] == "some_name_up_to_you") { 
     $found = true; 
     $exploded[1]++; 
     $lines[$k] = implode("|", $exploded); 
     break; // ??? 
    }   
} 

if (!$found) { 
    $lines[] = "THE_NEW_SITE|1"; 
} 

... 
+0

伟大的作品谢谢你,有没有办法,我可以检查,如果线路先退出,增量或添加新行? –

+0

没问题,欢迎来到SO。给我第二个找出你说的话:) – Ben

+0

好,我通常使用MySQL作为后端,但即时通讯与我的主机问题,所以即时通讯使用文本文件,直到它被分类。 –

0

一只手,另一只手你是只写在第一行到您的文件$a ...它让我迷惑你有什么在你.txt文件...

试试这个下面的代码...希望这本书能解决你的问题......

$filename = 'a.txt'; 
// get file contents and split it... 
$data = explode('|',file_get_contents($filename)); 
// increment the counting number... 
$data[1]++; 
// join the contents... 
$data = implode('|',$data); 
file_put_contents($filename, $data); 
+0

我希望它为每行每个文件都有一个新的计数器 –

0

而不是创造的Y我们自己的结构在文本文件中,为什么不使用PHP数组来跟踪?你也应该适用适当的锁定以防止竞争条件:

function recordDownload($download, $counter = 'default') 
{ 
    // open lock file and acquire exclusive lock 
    if (false === ($f = fopen("$counter.lock", "c"))) { 
      return; 
    } 
    flock($f, LOCK_EX); 

    // read counter data 
    if (file_exists("$counter.stats")) { 
      $stats = include "$counter.stats"; 
    } else { 
      $stats = array(); 
    } 

    if (isset($stats[$download])) { 
     $stats[$download]++; 
    } else { 
     $stats[$download] = 1; 
    } 

    // write back counter data 
    file_put_contents('counter.txt', '<?php return ' . var_export($stats, true) . '?>'); 

    // release exclusive lock 
    fclose($f); 
} 

recordDownload('product1'); // will save in default.stats 
recordDownload('product2', 'special'); // will save in special.stats 
+0

因为我需要对计数器进行排序以显示最热门下载属性。 –

+0

@BrendanMullan在任何情况下,我确信你可以编写一个shell脚本,我希望我的答案的其他部分涵盖咨询锁定帮助。 –

+0

@BrendanMullan我用更通用的解决方案更新了我的答案;它会跟踪一个文件中的所有下载计数。 –

0

我个人建议使用json blob作为文本文件的内容。那么你可以读取文件到PHP中,解码它(json_decode),处理数据,然后重新保存它。