Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. <?php
  2. $hostname = "localhost";
  3. $database = "paip_zam12017";
  4. $username = "paip_zam12017";
  5. $password = "zam12017";
  6. input_function("success.php");
  7. //Getting last id from database
  8. function last_id()
  9. {
  10. mysqli_report(MYSQLI_REPORT_STRICT);
  11. try
  12. {
  13. $hostname = "localhost";
  14. $database = "paip_zam12017";
  15. $username = "paip_zam12017";
  16. $password = "zam12017";
  17. $conn = new mysqli($hostname, $username, $password, $database);
  18. $conn->set_charset("utf8");
  19. if ($conn->connect_errno!=0)
  20. {
  21. throw new Exception(mysqli_connect_errno());
  22. }
  23. else
  24. {
  25. $sql = "SELECT ID FROM ogloszenia ORDER BY ID DESC LIMIT 1";
  26. if($result = $conn->query($sql))
  27. {
  28. if($conn->connect_error)
  29. {
  30. throw new Exception("Connection failed");
  31. //die("Connection failed");
  32. }
  33. if ($result->num_rows >= 1)
  34. {
  35. $row = $result->fetch_assoc();
  36. $returning = $row['ID'];
  37. $result->free_result();
  38. }
  39. else
  40. {
  41. $returning = 0;
  42. }
  43. return $returning;
  44. exit();
  45. }
  46. else
  47. {
  48. throw new Exception($conn->error);
  49. }
  50. $conn->close();
  51. }
  52. }
  53. catch(Exception $e)
  54. {
  55. return -1;
  56. }
  57. //Returning last id from database
  58. }
  59. //Adding file
  60. function addfile($id, $plik)
  61. {
  62. // $patch = str_replace('input.script.php', '', $_SERVER['SCRIPT_FILENAME'])."media/$id/";
  63. // $f['type'] == 'image/png' or $f['type'] == 'image/jpeg' or $f['type'] == 'image/gif'
  64. ////XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  65. error_reporting(0);
  66. $pathdir="./media/".$id; // change your directory name
  67. mkdir("./media/".$id);
  68. if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif")))
  69. {
  70. if($_FILES["file"]["size"] < 4000000)
  71. {
  72. if ($_FILES["file"]["error"] > 0)
  73. {
  74. echo "Error: " . $_FILES["file"]["error"] . "<br />";
  75. return -3;
  76. }
  77. else
  78. {
  79. $path="media/".$id."/".$_FILES["file"]['name'];
  80. copy( $_FILES["file"]['tmp_name'], $path );
  81. return $path;
  82. #echo "<div class='table'><table><tr><Td>Upload:</td><td> " . $_FILES["file"]["name"] . "</td></tr>";
  83. #echo "<tr><td>Type:</td><td> " . $_FILES["file"]["type"] . "</td></tr>";
  84. #echo "<tr><td>Size: </td><td>" . ($_FILES["file"]["size"] / 1024) . " Kb</td></tr>";
  85. #echo "<tr><td>Stored in:</td><td> " .$pathdir."/".$_FILES["file"]["name"]."</td></tr></table></div>";
  86. }
  87. }
  88. else
  89. {
  90. #echo "Upload files less than 4000kb";
  91. return -1;
  92. }
  93. }
  94. else
  95. {
  96. #echo "Invalid file format";
  97. return -2;
  98. }
  99. //Returning path to file
  100. }
  101. //Adding data to database
  102. function adddata($id, $path)
  103. {
  104. mysqli_report(MYSQLI_REPORT_STRICT);
  105. try
  106. {
  107. $hostname = "localhost";
  108. $database = "paip_zam12017";
  109. $username = "paip_zam12017";
  110. $password = "zam12017";
  111. $conn = new mysqli($hostname, $username, $password, $database);
  112. $conn->set_charset("utf8");
  113. if ($conn->connect_errno!=0)
  114. {
  115. throw new Exception(mysqli_connect_errno());
  116. }
  117. else
  118. {
  119. $w1 = $_POST['imie']; #imie
  120. $w2 = $_POST['nazwisko']; #nazwisko
  121. $w3 = $_POST['telefon']; #telefon
  122. $w4 = $_POST['mail']; #Email
  123. $w5 = $_POST['opis']; #opis
  124. $w6 = $_POST['status']; #zaginiony czy znaleziony
  125. $w7 = $path; #Photo
  126. $sql = sprintf('INSERT INTO ogloszenia(id, imie, nazwisko, numer, email, opis, status, Photo) VALUES ("%s","%s","%s","%s","%s","%s","%s","%s")',$id,mysqli_real_escape_string($conn, $w1),mysqli_real_escape_string($conn, $w2),mysqli_real_escape_string($conn, $w3),mysqli_real_escape_string($conn, $w4),mysqli_real_escape_string($conn, $w5),mysqli_real_escape_string($conn, $w6),mysqli_real_escape_string($conn, $w7));
  127. if($result = $conn->query($sql))
  128. {
  129. if($conn->connect_error)
  130. {
  131. throw new Exception("Connection failed");
  132. //die("Connection failed");
  133. }
  134. return 0;
  135. exit();
  136. }
  137. else
  138. {
  139. throw new Exception($conn->error);
  140. }
  141. $conn->close();
  142. }
  143. }
  144. catch(Exception $e)
  145. {
  146. return -1;
  147. }
  148. //Returning error
  149. }
  150. //Main Function
  151. function input_function($location)
  152. {
  153. if (isset($_POST['submit']) == true)
  154. {
  155. if ((isset($hostname) && isset($database) && isset($username) && isset($password))) # D#1
  156. {
  157. echo "Błąd! (D#1)";
  158. exit();
  159. }
  160. $id = last_id();
  161. if ($id == -1)
  162. {
  163. echo "Błąd! (#3)";
  164. exit();
  165. }
  166. $id = $id + 1;
  167. $path = addfile($id, $_FILES['file']);
  168. if ($path == -2) # #2
  169. {
  170. echo "Błąd! (#2)";
  171. exit();
  172. }
  173. else if ($path == -1) # #5
  174. {
  175. echo "Błąd! (#5)";
  176. exit();
  177. }
  178. else if ($path == -3)# #6
  179. {
  180. echo "Błąd! (#6)";
  181. exit();
  182. }
  183. //$path = str_replace('/home/paip/domains/paip.com.pl/public_html/c/2017/1/app/', 'http://c.paip.com.pl/2017/1/app/', $path);
  184. $path = "http://vc.paip.com.pl/s/".$path;
  185. $err = adddata($id, $path);
  186. if ($err == -1) # #4
  187. {
  188. echo "Błąd! (#4)";
  189. exit();
  190. }
  191. header("Location: $location");
  192. exit();
  193. }
  194. else # #1
  195. {
  196. echo "Błąd! (#1)";
  197. exit();
  198. }
  199. }
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement