Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form action="index.php" method="POST" enctype="multipart/form-data">
  6. Enter text:<br/>
  7. <input type="text" name="text" id="text"><br/><br/>
  8. Select image to upload:<br/>
  9. <input type="file" name="image" id="image"><br/><br/>
  10. <input type="submit" value="Send" name="submit">
  11. </form>
  12.  
  13.  
  14.  
  15. <?php
  16.  
  17. echo "aaaa<br>";
  18. if(isset($_REQUEST["submit"])){
  19. echo "requested submit<br>";
  20. echo "connecting db<br>";
  21. $conn = dbConnect();
  22. if(!$conn)
  23. echo "Error with connection to database";
  24.  
  25. echo "connected<br>";
  26. $text = $_REQUEST["text"];
  27. echo $text."<br>";
  28. if(isset($_FILES['image'])){
  29. $allowed_ext= array('jpg','jpeg','png','gif');
  30. $file_name =$_FILES['image']['name'];
  31. $file_ext = strtolower( end(explode('.',$file_name)));
  32.  
  33. $file_size=$_FILES['image']['size'];
  34. $file_tmp= $_FILES['image']['tmp_name'];
  35.  
  36. $type = pathinfo($file_name, PATHINFO_EXTENSION);
  37. $data = file_get_contents($file_tmp);
  38. $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
  39.  
  40. if(in_array($file_ext,$allowed_ext) === false)
  41. {
  42. echo "Extension not allowed";
  43. }
  44.  
  45. $image = $base64;
  46.  
  47. }else{
  48. $image = null;
  49. }
  50.  
  51. echo "preparing<br>";
  52. $stmt = $conn->prepare("INSERT INTO images(text,image) VALUES (?, ?)");
  53. $stmt->bind_param('ss', $text, $image);
  54. $stmt->execute();
  55. $stmt->close();
  56. $conn->close();
  57.  
  58. echo "Data sent!";
  59.  
  60. }
  61.  
  62. function dbConnect()
  63. {
  64. echo "trying connect<br>";
  65. /* $vcap_services = json_decode($_ENV["VCAP_SERVICES" ]);
  66. if($vcap_services->{'mysql-5.5'}){ //if "mysql" db service is bound to this application
  67. $db = $vcap_services->{'mysql-5.5'}[0]->credentials;
  68. }
  69. else if($vcap_services->{'cleardb'}){ //if cleardb mysql db service is bound to this application
  70. $db = $vcap_services->{'cleardb'}[0]->credentials;
  71. }
  72. else {
  73. echo "Error: No suitable MySQL database bound to the application. <br>";
  74. return false;
  75. }
  76. $mysql_database = $db->name;
  77. $mysql_port=$db->port;
  78. $mysql_server_name =$db->hostname . ':' . $db->port;
  79. $mysql_username = $db->username;
  80. $mysql_password = $db->password;
  81.  
  82. $conn = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database);
  83. if ($conn->connect_errno) {
  84. echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
  85. return false;
  86. */
  87. // Select database
  88. //$conn->select_db("images");
  89.  
  90.  
  91. $mysqli = new mysqli("sl-eu-lon-2-portal.10.dblayer.com:26468", "admin", "TJKVQSLJBWXBFXRH", "images");
  92. echo "connecting fction called <br>";
  93. if ($mysqli->connect_errno) {
  94. echo "failed connect<br>";
  95. return false;
  96. }
  97. echo "echo connected!<br>";
  98.  
  99. return $mysqli;
  100. }
  101.  
  102. ?>
  103.  
  104. </body>
  105. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement