-3

How to make download counter with php? the counter function was worked properly but download function can't get the file from database

the problem is here " print "window.location='../d_doc/$data[file]'"; "

please help me.

this is my download.php

    <?php
require_once 'config.php';

if(isset($_POST['gg']) ){
$download = $_POST['gg'];
$sql2="SELECT file from download";
$res2 = mysql_query($sql2);
$data = mysql_fetch_array($res2);
$sql = "UPDATE download
SET  ukuran_file = ukuran_file+1
WHERE id = '$download'";
$res = mysql_query($sql);
if($res){
echo'Edit Data Success!';
print "<script>window.location='../d_doc/$data[file]'</script>";
}

else{
echo'Edit Data Failed!<br/>';
echo mysql_error();
}
}

echo '<br/>';





?>
Henrikus Anthony
  • 154
  • 2
  • 4
  • 12

3 Answers3

0

If I understand your problem correctly, instead of 'print ... ' you can try redirecting the user to the download loacation as explained here : https://stackoverflow.com/a/768472

Community
  • 1
  • 1
Sachin
  • 33
  • 4
0

You have mess codes. try this:

<?php
require_once 'config.php';

if(isset($_POST['gg']) ){
    $file2download = $_POST['gg'];
    //start to fetch from $file2download
    $sql2="SELECT * FROM download WHERE file='$file2download'";
    $res2 = mysql_query($sql2);
    $data = mysql_fetch_array($res2);
    if ($data>0){
        $sql = "UPDATE download SET ukuran_file = ukuran_file+1 WHERE id = '$file2download'";
        $res = mysql_query($sql);
        if($res){
        echo "Edit Data Success!";
        echo "<meta http-equiv='refresh' content='0; url=../d_doc/"WHAT-IS-THIS">";
        }
    }
else{
    echo'Edit Data Failed!<br/>';
    echo mysql_error();
    }
}
echo '<br/>';
?>

Next time you should change to PDO or mysqli for better security and easy coding

don magug
  • 332
  • 1
  • 12
-3

if you want this with html file then you can use like this

<a href="path/to/file" download>Click here to download</a>
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68