Advertisement
jcarrascal

Contador de descargas

Sep 3rd, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. DROP TABLE IF EXISTS `descargas`;
  5. CREATE TABLE  `descargas` (
  6.   `ruta` varchar(200) NOT NULL,
  7.   `descargas` int(10) unsigned NOT NULL,
  8.   PRIMARY KEY (`ruta`)
  9. );
  10. */
  11.  
  12. $carpeta = '/ruta/a/las/descargas/';
  13. $hostname = 'localhost';
  14. $username = 'root';
  15. $password = '';
  16. $database = 'test';
  17.  
  18.  
  19. $archivo = basename(isset($_GET['archivo']) ? $_GET['archivo'] : '');
  20. $ruta = $carpeta . $archivo;
  21.  
  22. if (file_exists($ruta)) {
  23.  
  24.     $db = new MySQLi($hostname, $username, $password, $database);
  25.     if ($db->connect_error) {
  26.         die("Error de conexión ({$mysqli->connect_errno}): {$mysqli->connect_error}.");
  27.     }
  28.     $result = $db->query("insert into descargas (ruta, descargas) values
  29.         ('" . $db->escape_string($ruta) . "', 1) on duplicate key update descargas = descargas + 1");
  30.     if (false === $result) {
  31.         die("Error ({$db->errno}): {$db->error}.");
  32.     }
  33.  
  34.     header('Pragma: public');
  35.     header('Expires: 0');
  36.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  37.     header('Cache-Control: private');
  38.     header('Content-Type: application/octet-stream');
  39.     header('Content-Disposition: attachment; filename=' . $archivo);
  40.     header('Content-Transfer-Encoding: binary');
  41.     header('Content-Length: ' . filesize($ruta));
  42.     readfile($ruta);
  43.  
  44. } else {
  45.     echo "El archivo <b>$archivo</b> no existe. Notifíquelo al webmaster. Gracias";
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement