繁体   English   中英

计算每个用户的文件下载量,并使用PHP将其写入txt文件

[英]Count files downloads per user and write it in txt file using PHP

我正在尝试计算文件的下载量。 客户端单击链接后,在txt文件中写入用户名,路径和计数。

我的问题是:我如何才能将相同的用户以及他们在彼此之间单击的内容进行分组,因为现在我得到的是无序的:

用户名:Lee | 下载次数:50 | 下载文件:/ our-products / 88-series-led-wall-light

用户名:Nour | 下载次数:50 | 下载文件:/ our-products / 88-series-led-wall-light

用户名:Lee | 下载次数:50 | 下载文件:/ our-products / 88-series-led-wall-light

用户名:X | 下载次数:50 | 下载文件:/ our-products / 88-series-led-wall-light

用户名:Lee | 下载次数:50 | 下载文件:/ our-products / 88-series-led-wall-light

if( isset($_GET['count'])){
    $username = $_GET['username'];
    $_SESSION['count'] = $_SESSION['count']+1;


    if(file_exists('countClick_images.txt')){
        $myFile = fopen('countClick_images.txt', 'w') or die('unable to open file');
        $txt = 'Username: ' .$username.' | Number of download: '.$_SESSION['count'].' | File Downloaded:'.$path ;
        fwrite($myFile,"\n". $txt);
        fclose($myFile);
    }else {
        $myfile = fopen("countClick_images.txt", "a") or die("Unable to open file!");
        $txt = 'Username: ' .$username.' | Number of download: '.$_SESSION['count'].' | File Downloaded:'.$path ;
        fwrite($myfile, "\n". $txt);
        fclose($myfile);
    }
}

单文件版本

考虑到评论中的一些建议,我会做这样的事情。

您可以将序列化的信息存储在文件中,因此可以轻松地将其还原为php变量。

我不会存储“计数”,因为如果您有已下载项目的列表,那么这是多余的信息。 您可以随时计算该数组。

首先,我运行此脚本以使用您提供的数据初始化文件。

<?php
// Only first time to load initial data in file
$data = [
    'Lee' => [
        'files' => [
            '/our-products/88-series-led-wall-light',
            '/our-products/88-series-led-wall-light',
            '/our-products/88-series-led-wall-light'
        ]
    ],
    'Nour' => [
        'files' => [
            '/our-products/88-series-led-wall-light'
        ]
    ],
    'X' => [
        'files' => [
            '/our-products/88-series-led-wall-light'
        ]
    ]
];

$file = 'countClick_images.txt';
file_put_contents($file, serialize($data));

这将生成具有以下序列化数据的文件:

a:4:{s:3:"Lee";a:1:{s:5:"files";a:3:{i:0;s:38:"/our-products/88-series-led-wall-light";i:1;s:38:"/our-products/88-series-led-wall-light";i:2;s:38:"/our-products/88-series-led-wall-light";}}s:4:"Nour";a:1:{s:5:"files";a:1:{i:0;s:38:"/our-products/88-series-led-wall-light";}}s:1:"X";a:1:{s:5:"files";a:1:{i:0;s:38:"/our-products/88-series-led-wall-light";}}s:8:"testUser";a:1:{s:5:"files";a:1:{i:0;s:10:"/test/file";}}}

之后,我修改了您的脚本以使用serialize()unserialize()

$storeFile = 'countClick_images.txt';

// I forced some GET data here to try
$_GET['username'] = 'testUser';
$_GET['path'] = '/test/file';

if (isset($_GET['username']) && isset($_GET['path'])) {

    $username = $_GET['username'];
    $downloadedFile = $_GET['path'];

    if (file_exists($storeFile)) {
        $data = unserialize(file_get_contents($storeFile));
    } else {
        $data = [];
    }

    echo "Data before:\n" . print_r($data,1);

    // This creates a particular user data structure
    // I put the files array into a 'files' key for you to be able to store more information of that user

    if (!isset($data[$username])) {
        $data[$username] = ['files' => []];
    }

    $data[$username]['files'][] = $downloadedFile;

    echo "Data after:\n" . print_r($data,1);

    // This creates the file if it doesn't exist
    file_put_contents($storeFile, serialize($data));
}

另外,我认为将每个用户的所有信息存储在一个文件中是一个好主意,以避免在用户和下载量很多的情况下使用大文件。

多文件版本 (也使用用户ID代替用户名作为索引)

为了避免潜在的问题,我制作了第二版的实际操作方法:巨大的单个文件和错误的用户名。

该脚本会使用您提供的初始数据生成三个不同的文件,每个用户一个文件。

<?php
// Only first time to load initial data in file
$data = [
    '1' => [
        'username' => 'Lee',
        'files' => [
            '/our-products/88-series-led-wall-light',
            '/our-products/88-series-led-wall-light',
            '/our-products/88-series-led-wall-light'
        ]
    ],
    '2' => [
        'username' => 'Nour',
        'files' => [
            '/our-products/88-series-led-wall-light'
        ]
    ],
    '3' => [
        'username' => 'X',
        'files' => [
            '/our-products/88-series-led-wall-light'
        ]
    ]
];

foreach ($data as $userId => $userData) {
    $fileName = 'countClick_images_user_' . $userId . '.txt';
    file_put_contents($fileName, serialize($userData));
}

现在您有了以下文件:

countClick_images_user_1.txt
countClick_images_user_2.txt
countClick_images_user_3.txt

之后,用于添加新下载内容的脚本如下所示:

<?php

$_GET['userid'] = '4';
$_GET['path'] = '/test/file';
$_GET['username'] = 'testUser'; // You need this input name only first time a user downloads something

if (isset($_GET['userid']) && isset($_GET['username']) && isset($_GET['path'])) {

    // You can add more controls here to validate input data
    $userId = $_GET['userid'];
    $userName = $_GET['username'];
    $downloadedFile = $_GET['path'];

    $storeFile = 'countClick_images_user_' . $userId . '.txt';

    if (file_exists($storeFile)) {
        $data = unserialize(file_get_contents($storeFile));
    } else {
        $data = [];
    }

    echo "Data before:\n" . print_r($data,1);

    // Here you create data structure
    if (!isset($data['username'])) {
        $data['username'] = $userName;
        $data['files'] = [];
    }

    $data['files'][] = $downloadedFile;

    echo "Data after:\n" . print_r($data,1);

    file_put_contents($storeFile, serialize($data));
}

首次运行时,它将为用户4创建一个新文件,创建数据结构,添加其首次下载并将其存储到文件中。

如果再次运行,它将获取用户的4个文件的数据,添加新的下载并将其再次存储在文件中。

输出所有用户信息:

<?php

// Format:
// * username: nour | clicks: 55 | files: file1, file2, file3 * lee | clicks 5| files: file1, file3 * x | clicks 22| files: file3

// glob returns an array with all file names that match that pattern
foreach (glob("countClick_images_user_*.txt") as $storeFile) {

    $userData = unserialize(file_get_contents($storeFile));

    echo "username: " . $userData['username'] . " | ";
    echo "clicks: " . count($userData['files']) . " | ";
    echo "files: " . implode(",", $userData['files']);
    echo "\n";
}

这将输出以下内容:

username: Lee | clicks: 3 | files: /our-products/88-series-led-wall-light, /our-products/88-series-led-wall-light, /our-products/88-series-led-wall-light
username: Nour | clicks: 1 | files: /our-products/88-series-led-wall-light
username: X | clicks: 1 | files: /our-products/88-series-led-wall-light
username: testUser | clicks: 2 | files: /test/file, /test/file

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM