简体   繁体   English

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

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

I am trying to count the downloads of files. 我正在尝试计算文件的下载量。 Once the client click a link write in a txt file the username and path and number of counts. 客户端单击链接后,在txt文件中写入用户名,路径和计数。

my problem is: How can I make it to group same users and what they clicked under each other because now I am getting this with no order: 我的问题是:我如何才能将相同的用户以及他们在彼此之间单击的内容进行分组,因为现在我得到的是无序的:

Username: Lee | 用户名:Lee | Number of download: 50 | 下载次数:50 | File Downloaded:/our-products/88-series-led-wall-light 下载文件:/ our-products / 88-series-led-wall-light

Username: Nour | 用户名:Nour | Number of download: 50 | 下载次数:50 | File Downloaded:/our-products/88-series-led-wall-light 下载文件:/ our-products / 88-series-led-wall-light

Username: Lee | 用户名:Lee | Number of download: 50 | 下载次数:50 | File Downloaded:/our-products/88-series-led-wall-light 下载文件:/ our-products / 88-series-led-wall-light

Username: X | 用户名:X | Number of download: 50 | 下载次数:50 | File Downloaded:/our-products/88-series-led-wall-light 下载文件:/ our-products / 88-series-led-wall-light

Username: Lee | 用户名:Lee | Number of download: 50 | 下载次数:50 | File Downloaded:/our-products/88-series-led-wall-light 下载文件:/ 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);
    }
}

Single file version 单文件版本

Having in mind some suggestions in comments I would do something like this. 考虑到评论中的一些建议,我会做这样的事情。

You can store the information serialized in the file, so you can restore it easily to a php variable. 您可以将序列化的信息存储在文件中,因此可以轻松地将其还原为php变量。

I wouldn't store "count" because it's a redundant information if you have the list of downloaded items. 我不会存储“计数”,因为如果您有已下载项目的列表,那么这是多余的信息。 You can count that array always you want. 您可以随时计算该数组。

First I run this script to initialize the file with the data you gave. 首先,我运行此脚本以使用您提供的数据初始化文件。

<?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));

This generates a file with this serialized 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";}}}

After that, I modified your script to use serialize() and unserialize() . 之后,我修改了您的脚本以使用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));
}

Also, I think it would be a good idea to store all the information in one file per user in order to avoid the use of a huge file if you have many users and downloads. 另外,我认为将每个用户的所有信息存储在一个文件中是一个好主意,以避免在用户和下载量很多的情况下使用大文件。

Multi file version (Also using user id instead of user names as index) 多文件版本 (也使用用户ID代替用户名作为索引)

I made a second version of how I would really do this, in order to avoid to potential issues: huge single file and bad usernames. 为了避免潜在的问题,我制作了第二版的实际操作方法:巨大的单个文件和错误的用户名。

This script generates three different files with the initial data you gave, one file per user. 该脚本会使用您提供的初始数据生成三个不同的文件,每个用户一个文件。

<?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));
}

Now you have these files: 现在您有了以下文件:

countClick_images_user_1.txt
countClick_images_user_2.txt
countClick_images_user_3.txt

After that, the script to add new downloads would look like this: 之后,用于添加新下载内容的脚本如下所示:

<?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));
}

First time you run it, it creates a new file for user 4, creates the data structure, add its first download and stores it into the file. 首次运行时,它将为用户4创建一个新文件,创建数据结构,添加其首次下载并将其存储到文件中。

If you run it again, it takes the data of user's 4 file, add the new download and stores it again in the file. 如果再次运行,它将获取用户的4个文件的数据,添加新的下载并将其再次存储在文件中。

Output all users info: 输出所有用户信息:

<?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";
}

This will output this: 这将输出以下内容:

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