Advertisement
etandel

upload.php

Dec 18th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. <?php
  2. session_start();
  3. // file has been uploaded?
  4. if(isset($_FILES['fileuploads']))
  5. {
  6. // There is any error?
  7. if($_FILES['fileuploads']['error'] == 0)
  8. {
  9. // Database connection
  10. $dbconn = new mysqli('mysql17.000webhost.com', 'a2083177_mydb', 'password1', 'a2083177_mydb');
  11. if(mysqli_connect_errno())
  12. {
  13. die("MySQL connection failed: ". mysqli_connect_error());
  14. }
  15.  
  16. // All required data
  17. $id = $dbconn->real_escape_string($_SESSION['user_id']);
  18. $name = $dbconn->real_escape_string($_FILES['fileuploads']['name']);
  19. $type = $dbconn->real_escape_string($_FILES['fileuploads']['type']);
  20. $data = $dbconn->real_escape_string(file_get_contents($_FILES ['fileuploads']['tmp_name']));
  21. $size = intval($_FILES['fileuploads']['size']);
  22.  
  23. $fileName = $_FILES['fileuploads']['name']; // The file name
  24. $fileTmpLoc = $_FILES["fileuploads"]["tmp_name"]; // File in the PHP tmp folder
  25. move_uploaded_file($fileTmpLoc, "uploads/$fileName");
  26.  
  27. //move_uploaded_file($_FILES['fileuploads']['name'] , "fileuploads/$name" );
  28. // Create the SQL query
  29. $query = "
  30. INSERT INTO `files` (`id`,`u_id`,`name`, `type`, `size`, `data`, `created`)
  31. VALUES ('{$id}','{$id}','{$name}', '{$type}', {$size}, '{$data}', NOW())";
  32.  
  33. // Execute the query
  34. $result = $dbconn->query($query);
  35.  
  36. // Check if it was successfull
  37. if($result)
  38. {
  39. header("location: homepage.php");
  40. echo 'Success! Your file was successfully uploaded!';
  41. }
  42. else
  43. {
  44. echo 'Error! Failed to upload the file'
  45. . "<pre>{$dbconn->error}</pre>";
  46. }
  47. }
  48. else
  49. {
  50. echo 'An error while the file was being uploaded. '
  51. . 'Error code: '. intval($_FILES['fileuploads']['error']);
  52. }
  53.  
  54. // Close the mysql connection
  55. $dbconn->close();
  56. }
  57. else
  58. {
  59. echo 'Error! A file was not sent!';
  60. }
  61.  
  62. // Echo a link back to the main page
  63. echo '<p>Click <a href="homepage.php">here</a> to go back</p>';
  64. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement