سؤال

كيف يمكنني عرض عداد يحسب عدد المرات التي يتم فيها تنزيل ملف؟ لقد رأيته من قبل. "تم تنزيل 450 مرة". شكرًا.

هل كانت مفيدة؟

المحلول

لا تدع المستخدم يقوم بتنزيل ملف مباشرة ، لكن نصوصًا مثل ما يلي ...

<?php

   $file = $_REQUEST['file'];
   $dldir = "downloads/";

   if (
       (file_exists($dldir.$file) &&               // file exists
       (strpos($file, "../") === false) &&  // prevent an attacker from switching to a parent directory
      ) {

       header('Content-type: '.mime_content_type($dldir.file));
       header("Content-Transfer-Encoding: binary");
       header("Content-Length: " . filesize($dldir.$file) ."; "); 
       header('Content-Disposition: attachment; filename="'.$file.'"');

       echo file_get_contents($dldir.$file);

       /** Update the counter here, e.g. by using mysql **/
   } else {
       die("File not found");
   }

?>

نصائح أخرى

إذا كنت ترغب في القيام بذلك مع PHP ، فأنت بحاجة إلى التحكم في التنزيل في برنامج نصي PHP. في الأساس ، يتعلق الأمر بالخطين التاليين من الرمز الزائف:

set_number_of_downloads(get_number_of_downloads() + 1);
readfile($file_being_downloaded);

هناك اذهب أنت. أيضًا ، إذا كنت تفضل استخدام MySQL من أجل الثبات ، فهناك هذه المحلول.

على Apache ، يمكن أن يكون لديك قاعدة بيانات Mod_Rewrite عند طلب الملف. هذا له ميزة السرعة في الإرسال (يمكن استخدام SendFile) ، وليس عليك تغيير عناوين URL أو بنية الدليل.

#!/usr/bin/perl
$| = 1;
$dbh = DBI->connect("dbi:mysql:database=; host=localhost; user=; password=")
or die "Connecting from PHP to MySQL database failed: $DBI::errstr";
while (<STDIN>) {
    $dbh->query(... update database ...);
    print $_;
}

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteengine

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top