Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <?php
  2. include($_SERVER['DOCUMENT_ROOT']."/connection.php"); // Paste the connection to SQL
  3. $filename = mysqli_real_escape_string($conn,$_GET['file']);
  4. $path = $_SERVER['DOCUMENT_ROOT']."/"; //Path to the file
  5. $fullPath = $path.$filename; //Path to the download file
  6. $ip =$_SERVER['REMOTE_ADDR'];
  7. $firmware = $_SERVER['HTTP_X_FIRMWARE'];
  8. $device = $_SERVER["HTTP_X_MACHINE"];
  9. $agent = $_SERVER["HTTP_USER_AGENT"];
  10. $filetypes = array("deb"); // Supported file types
  11.  
  12. if (!in_array(substr($filename, -3), $filetypes)) {
  13. echo "Incorrect file type.";
  14. exit;
  15. }
  16.  
  17. if ($fd = fopen ($fullPath, "r")) {
  18. //add download stat
  19. $result = mysqli_query($conn,$sqq);
  20. $sqq="SELECT COUNT(*) AS countfile FROM download WHERE filename='" . $filename . "'");
  21. $data = mysqli_fetch_array($result);
  22. $q = "";
  23. if ($data['countfile'] > 0) {
  24. $q = "UPDATE download SET device = '$device', firmware = '$firmware', agent = '$agent', ip = '$ip', dldate = NOW(), stats = stats + 1 WHERE
  25. filename = '" . $filename . "'";
  26. } else {
  27. $q = "INSERT INTO download (filename, dldate, stats, ip, agent, firmware, device) VALUES
  28. ('" . $filename . "',NOW(), 1, '$ip', '$agent', '$firmware', '$device')";
  29. }
  30. $statresult = mysqli_query($conn,$q);
  31. //the nearest part leads from the file
  32. $fsize = filesize($fullPath);
  33. $path_parts = pathinfo($fullPath);
  34.  
  35. header("Content-type: application/octet-stream");
  36. header("Content-Disposition: filename="".$path_parts["basename"].""");
  37. header("Content-length: $fsize");
  38. header("Cache-control: private"); //Open the download file
  39. while(!feof($fd)) {
  40. $buffer = fread($fd, 2048);
  41. echo $buffer;
  42. }
  43. }
  44. fclose ($fd);
  45. exit;
  46. ?>
  47.  
  48. // Create connection
  49. $servername = "localhost";
  50. $username = "user";
  51. $password = "pass";
  52. $db = "database";
  53.  
  54. $conn = mysqli_connect($servername, $username, $password, $db);
  55.  
  56. // Check connection
  57. if (!$conn) {
  58. die("Connection failed: " . mysqli_connect_error());
  59. }
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement