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

时间:2017-07-29 09:15:25

标签: php

我正在尝试计算文件的下载量。一旦客户端点击链接在txt文件中写入用户名和路径以及计数。

我的问题是:我如何才能将相同的用户以及他们互相点击的内容组合在一起,因为现在我得到的却没有订单:

用户名:Lee |下载次数:50 |文件已下载:/我们的产品/ 88系列led-wall-light

用户名:Nour |下载次数:50 |文件已下载:/我们的产品/ 88系列led-wall-light

用户名:Lee |下载次数:50 |文件已下载:/我们的产品/ 88系列led-wall-light

用户名:X |下载次数:50 |文件已下载:/我们的产品/ 88系列led-wall-light

用户名:Lee |下载次数:50 |文件已下载:/我们的产品/ 88系列led-wall-light

void

2 个答案:

答案 0 :(得分:0)

单个文件版本

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

您可以将序列化的信息存储在文件中,这样您就可以轻松地将其恢复为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));
}

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

答案 1 :(得分:0)

多文件版本(也使用用户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