Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1.  
  2.  
  3. <?php
  4. // Check if a file has been uploaded
  5. if(isset($_FILES['upload'])) {
  6. // Make sure the file was sent without errors
  7. if($_FILES['upload']['error'] == 0) {
  8. // Connect to the database
  9. $dbLink = new mysqli('mysql.hostinger.co.uk', 'u327251486_image', 'password', 'u327251486_store');
  10. if(mysqli_connect_errno()) {
  11. die("MySQL connection failed: ". mysqli_connect_error());
  12. }
  13.  
  14. // Gather all required data
  15. $name = $dbLink->real_escape_string($_FILES['upload']['name']);
  16. $mime = $dbLink->real_escape_string($_FILES['upload']['type']);
  17. $data = $dbLink->real_escape_string(file_get_contents($_FILES ['upload']['tmp_name']));
  18. $size = intval($_FILES['upload']['size']);
  19.  
  20. // Create the SQL query
  21. $query = "
  22. INSERT INTO `file` (
  23. `name`, `mime`, `size`, `data`, `created`
  24. )
  25. VALUES (
  26. '{$name}', '{$mime}', {$size}, '{$data}', NOW()
  27. )";
  28.  
  29. // Execute the query
  30. $result = $dbLink->query($query);
  31.  
  32. // Check if it was successfull
  33. if($result) {
  34. echo 'Success! Your file was successfully added!';
  35. }
  36. else {
  37. echo 'Error! Failed to insert the file'
  38. . "<pre>{$dbLink->error}</pre>";
  39. }
  40. }
  41. else {
  42. echo 'An error accured while the file was being uploaded. '
  43. . 'Error code: '. intval($_FILES['upload']['error']);
  44. }
  45.  
  46. // Close the mysql connection
  47. $dbLink->close();
  48. }
  49. else {
  50. echo 'Error! A file was not sent!';
  51. }
  52.  
  53. // Echo a link back to the main page
  54. echo '<p>Click <a href="index.html">here</a> to go back</p>';
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement