我有这个 php 页面,它假设显示一个图像,然后在每次访问时使用 1 个数字更新数据库中的“下载”行,有时这有效,而对于其他一些图片它不起作用,我该如何解决这个问题?如果有问题,请查看我的代码:
<?php
include 'conf_global.php';
if ($_GET['id'])
{
$id = $_GET['id'];
}
else
{
die ("no id selected");
}
@ $db = mysql_pconnect($mysql['host'], $mysql['user'], $mysql['pass']) or die(mysql_error());
//IT"S NOT WORKING!
if (!$db)
{
die("error");
}
mysql_select_db($mysql['db']) or die(mysql_error());
$query = "SELECT * FROM `images` WHERE id='" . $id ."'";
$result = mysql_query($query) or die(mysql_error());
if (!$result)
{
die("MySQL Select error");
}
$num_results = mysql_num_rows($result);
if ($num_results ==0)
{
die("Image not found");
}
$row = mysql_fetch_array($result);
if ($id != 0)
{
$downloads = $row['downloads'] + 1;
$lastuse = time();
$ss = mysql_query("select downloads from `images` where id='".$id."'") or die(mysql_error());
$rr = mysql_fetch_array($ss);
$query = "update `images` set downloads='".($rr['downloads']+1)."' where id='".$id."'";
$result = mysql_query($query);
if (!$result)
{
die("MySQL update error");
}
$query = "UPDATE `images` SET lastuse='" . $lastuse . "' WHERE id='". $id ."'";
$result = mysql_query($query);
if (!$result)
{
die("MySQL update error2");
}
//get current stats
$query = "SELECT * FROM `stat_cache`";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Select error");
}
$stat = mysql_fetch_array($result);
//band update
$bandwidth = $stat['band'] + $row['size'];
$query = "UPDATE `stat_cache` SET band='" . $bandwidth . "' WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
}
//downloads update
$downloads = $stat['downloads'] + 1;
$query = "UPDATE `stat_cache` SET downloads='" . $downloads . "' WHERE 1";
$result = mysql_query($query);
if (!$result)
{
die("MySQL Update error");
}
}
//Lets create the image, now.
if(!file_exists('./uploads/' . $id)) {
die("Image not found");
exit;
}
header('Content-type: ' . $row['type']);
$fp = fopen('./uploads/' . $id, 'r');
$contents = fread($fp, $maxfilesize);
fclose($fp);
echo $contents;
?>