문제
파일이 다운로드 된 횟수를 계산하는 카운터를 어떻게 표시 할 수 있습니까? 전에 본 적이 있습니다. "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);
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
제휴하지 않습니다 StackOverflow