-3

I have a file that is downloaded though my site. I want to count the number of downloads. I have achieved it with the following code:

$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
$selected = mysql_select_db("a7871565_first",$dbhandle) 
  or die("Could not select examples");
$result = mysql_query("select * from downloads");
$row = mysql_fetch_array($result);
$count= $row{'download'};
$count= $count+1;
$result = mysql_query("UPDATE downloads SET download =$count");
echo "<script>";

    $url = "http://www.mohammediatechnologies.in/apps/TubeMateHd.apk";
    echo "window.open('$url');".PHP_EOL;

echo "</script>";

It does update the database with count as well as downloads the file but when the user clicks on the download button he is redirected to the file named download.php containing the code above. The user is presented with a blank screen which is something I don't want. The file download is opening in another window that is blocked as it is considered as a popup.

What I want to achieve is when the user clicks on the download link, the user stays on the same page, the database updates and the file is downloaded.

Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78
farooq shaik
  • 69
  • 1
  • 11
  • 2
    http://stackoverflow.com/questions/2882472/php-send-file-to-user - look into this, it will tell you how to make a php script that will send the file but not refresh the page for the user. – Lost F. Feb 06 '15 at 09:50
  • 1
    possible duplicate of [How do I display PHP code in HTML?](http://stackoverflow.com/questions/4842575/how-do-i-display-php-code-in-html) – B001ᛦ Feb 06 '15 at 09:50
  • 1
    @bub - Nope, but the question title is pretty fun, right? Can you imagine? – Bitwise Creative Feb 06 '15 at 09:52
  • Please paste in some code samples – Musa Haidari Feb 06 '15 at 09:53
  • @musa check tubemate.gq and press download 1. its taking me to blank page 2. download is blocked by popup of browser my goal is to stay in same page initiate download and also execute php code in download.php file – farooq shaik Feb 06 '15 at 09:58
  • So, If I am correct, you want to make the user download, it does not matter JS or PHP, also you want download.php to execute at the time of download. So I think this helps: http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php – Musa Haidari Feb 06 '15 at 10:13
  • 1
    @musa it helped me and its working how do i accept your comment as answer here – farooq shaik Feb 06 '15 at 10:22

1 Answers1

0

As I mentioned in the comments too, you want to make the user download, it does not matter JS or PHP, also you want download.php to execute at the time of download. So I think this may help you: How to force file download with PHP

Basically when you want to force download a file in a browser, you need to set proper headers to the browser, so it knows you want the user to download the file, and then send the data to the browser.

In PHP frameworks there are some helper function to help you do the same (eg. in codeigniter you can use force_download() to achieve the same task).

Community
  • 1
  • 1
Musa Haidari
  • 2,109
  • 5
  • 30
  • 53