[英]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.