Dropbox, the favorite cloud syncing option for millions of people, currently has no feature to track Public Downloads. This could be a very useful feature for many webmasters and bloggers, including me. Although it has been put up in the Votebox to request this and has been voted by 4,000+ people, it is still unclear if Dropbox will be getting this sometime soon in the future.
However, you use a simple external script to count the number of downloads and store it in a sql database which I’ll be showing in this post.
Demo
View the working tracking script in this page.
Download Test Files (on Dropbox): File 1 | File 2 | File 3
When you Download any file among the above 3, the counts will be updated.
The Script
(Download script as a zipped file)
First of all, copy the codes below and open a text editor of your choice like notepad++, Dreamweaver, FrontPage etc. (and NOT MS Word or WordPad) and paste:
[php]<br /><br />&lt;?php</p><br /><p>$host=&quot;localhost&quot;;<br /><br />$database=&quot;cpanel-username_database-name&quot;;<br /><br />$username=&quot;cpanel-username_database-name&quot;;<br /><br />$password=&quot;password&quot;;<br /><br />$table=&quot;track&quot;;</p><br /><p>//Get Passed value and store it<br /><br />$name = $_GET['url'];</p><br /><p>//Check if URL is a valid Dropbox url<br /><br />$cmp=strcmp(substr($name, 0, 17), &quot;http://dl.dropbox&quot;);<br /><br />if($cmp==0)<br /><br />{<br /><br /><%%KEEPWHITESPACE%%> //Finding Dropbox ID and File Location<br /><br /><%%KEEPWHITESPACE%%> $trmd=trim ( $name, &quot;http://dl.dropbox.com/u/&quot; );<br /><br /><%%KEEPWHITESPACE%%> $id_pos=mb_strpos ( $trmd , &quot;/&quot; );<br /><br /><%%KEEPWHITESPACE%%> $id=substr($trmd, 0, $id_pos);<br /><br /><%%KEEPWHITESPACE%%> $file=substr($trmd , $id_pos);</p><br /><p> // Connect and Create a table if it doesn't exist<br /><br /><%%KEEPWHITESPACE%%> mysql_connect($host,$username,$password);<br /><br /><%%KEEPWHITESPACE%%> @mysql_select_db($database) or die( &quot;Unable to select database&quot;);<br /><br /><%%KEEPWHITESPACE%%> $query=&quot;CREATE TABLE IF NOT EXISTS $table (id int(6) NOT NULL auto_increment,Name text NOT NULL,Downloads int NOT NULL,PRIMARY KEY (id))&quot;;<br /><br /><%%KEEPWHITESPACE%%> mysql_query($query);</p><br /><p> // Find the Position of that File Name<br /><br /><%%KEEPWHITESPACE%%> $query=&quot;SELECT * FROM $datanase.$table WHERE Name='$file'&quot;;<br /><br /><%%KEEPWHITESPACE%%> $result = mysql_query($query);<br /><br /><%%KEEPWHITESPACE%%> $row = mysql_fetch_array($result);<br /><br /><%%KEEPWHITESPACE%%> $pos=$row['id'];</p><br /><p> if($pos==NULL)<br /><br /><%%KEEPWHITESPACE%%> {<br /><br /><%%KEEPWHITESPACE%%> //Insert a new row into Database for a new download entry<br /><br /><%%KEEPWHITESPACE%%> mysql_query(&quot;INSERT INTO $datanase.$table VALUES ('', '$file', 1 );&quot;);<br /><br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> else<br /><br /><%%KEEPWHITESPACE%%> {<br /><br /><%%KEEPWHITESPACE%%> //Update Downloads Count if Position is Found<br /><br /><%%KEEPWHITESPACE%%> $sql = &quot;UPDATE $datanase.$table SET Downloads=Downloads +1 WHERE id=$pos&quot;;</p><br /><p> $result = mysql_query ($sql);<br /><br /><%%KEEPWHITESPACE%%> }</p><br /><p>}<br /><br />else {header (&quot;Location: $name&quot;); }</p><br /><p>if($name!=NULL)<br /><br /><%%KEEPWHITESPACE%%> {header (&quot;Location: $name&quot;); }<br /><br />else<br /><br /><%%KEEPWHITESPACE%%> {</p><br /><p> //Check if Table exists<br /><br /><%%KEEPWHITESPACE%%> mysql_connect($host,$username,$password);<br /><br /><%%KEEPWHITESPACE%%> mysql_select_db($database) or die(&quot;Nothing to display&quot;);<br /><br /><%%KEEPWHITESPACE%%> if((mysql_query(&quot;SELECT * FROM $database&quot;)))<br /><br /><%%KEEPWHITESPACE%%> {<br /><br /><%%KEEPWHITESPACE%%> echo &quot;Nothing to display&quot;;<br /><br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> else{</p><br /><p> //Displaying All the Info<br /><br /><%%KEEPWHITESPACE%%> mysql_connect($host,$username,$password);<br /><br /><%%KEEPWHITESPACE%%> @mysql_select_db($database) or die( &quot;Unable to select database&quot;);<br /><br /><%%KEEPWHITESPACE%%> $query=&quot;SELECT * FROM $database.$table ORDER BY id&quot;;<br /><br /><%%KEEPWHITESPACE%%> $result=mysql_query($query);</p><br /><p> $num=mysql_numrows($result);</p><br /><p> mysql_close();</p><br /><p> echo &quot;&lt;center&gt;&lt;h2&gt;&lt;center&gt;Dropbox Downloads Count&lt;/center&gt;&lt;/h2&gt;&lt;br&gt;&lt;br&gt;&quot;;<br /><br /><%%KEEPWHITESPACE%%> ?&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;table border=&quot;0&quot; cellspacing=&quot;2&quot; cellpadding=&quot;10&quot; border=&quot;2&quot; style=&quot;border-collapse: collapse; border: 1px solid #333; font-family: &quot;Trebuchet MS&quot;, Verdana, Arial, Helvetica, sans-serif; font-size: 0.9em; color: #000; background: #bcd0e4;&quot;&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;tr&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;th style=&quot;color: #fff; background-color: #7d98b3; border-right: 1px dotted #666;&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;Id&lt;/font&gt;&lt;/th&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;th style=&quot;color: #fff; background-color: #7d98b3; border-right: 1px dotted #666;&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;Name&lt;/font&gt;&lt;/th&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;th style=&quot;color: #fff; background-color: #7d98b3; border-right: 1px dotted #666;&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;Downloads&lt;/font&gt;&lt;/th&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;/tr&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;?php<br /><br /><%%KEEPWHITESPACE%%> $i=0;<br /><br /><%%KEEPWHITESPACE%%> while ($i &lt; $num) {<br /><br /><%%KEEPWHITESPACE%%> $id=mysql_result($result,$i,&quot;id&quot;);<br /><br /><%%KEEPWHITESPACE%%> $entry_name=mysql_result($result,$i,&quot;Name&quot;);<br /><br /><%%KEEPWHITESPACE%%> $downloads=mysql_result($result,$i,&quot;Downloads&quot;);<br /><br /><%%KEEPWHITESPACE%%> ?&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;tr&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;td style=&quot;background-color: #b8cfe5; border-right: 2px solid #333;&quot; &gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;&lt;? echo $id; ?&gt;&lt;/font&gt;&lt;/td&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;td style=&quot;background-color: #b8cfe5; border-right: 2px solid #333;&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;&lt;? echo $entry_name; ?&gt;&lt;/font&gt;&lt;/td&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;td style=&quot;background-color: #b8cfe5; border-right: 2px solid #333;&quot;&gt;&lt;font face=&quot;Arial, Helvetica, sans-serif&quot;&gt;&lt;? echo $downloads; ?&gt;&lt;/font&gt;&lt;/td&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;/tr&gt;<br /><br /><%%KEEPWHITESPACE%%> &lt;?php<br /><br /><%%KEEPWHITESPACE%%> $i++;<br /><br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> echo &quot;&lt;/table&gt;&lt;/center&gt;&quot;;<br /><br /><%%KEEPWHITESPACE%%> }<br /><br />}<br /><br />?&gt;<br /><br />[/php]
Step 1. Open cPanel and goto MySQL Database Wizard and create a new database. Then create a new user and grant it all permissions.
Step 2. Now, what you need to is edit lines 3 to 7 as following:
- $host=”localhost”; – Change “localhost” to your mySQL host location. (In most of the cases, you won’t be needing to change it)
- $database=”cpanel-username_database-name”; – Here, “cpanel-username_database-name” refers to the database name you just created. Change it as yourcpanelusername_databasename
- $username=”cpanel-username_database-username”; – Change this to your database username in the format –yourcpanelusername_databaseusername
- $password=”password”; – Change this to password. Leave it unaltered or change it if you wish to.
- $table=”track”; – Here, “track” refers to the table name. Change it if you wish to.
Step 3. When you’re done with editing part, Save this file as “track.php” (without the quotes) or any other name you wish to.
Step 4. Upload this file to wherever you like in your server. I would recommend the root folder so that you don’t have to type a long URL to access it.
Tada. Your tracker is ready. For tracking any File, call your track.php file in this way:
http://yoursite.com/track.php?url=URL TO THE FILE
When you want to see Download stats, just open track.php in your browser, i.e., :
http://yoursite.com/track.php
Some little Benefits:
- No Hassle of using a separate plugin (in case of a blog). A plugin will add more CSS and JS files resulting in more HTTP requests and finally slower loading speed of your blog/site.
- Only Dropbox (Public) Files are recorded into the database.
- Simple Script, small size, just edit the values and go.
Do More with it
Instead of just tracking downloads, you can do lots more with just few tweaks in the script. For example, you can create pie charts, bar graph etc. for seeing from which countries you’re files get downloaded the most etc.
JpGraph is a library for PHP nice for creating graphs (also try this peters1‘s script). For other stuffs, Just run your imagination!
I know many people may argue with me that this is not a proper way to track downloads. But see, there is no other possible and simpler way to do this job. And for those who get into any problem while using this script, leave a comment below and I’ll try my best to solve it 🙂