Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $hostname = "localhost";
- $database = "paip_zam12017";
- $username = "paip_zam12017";
- $password = "zam12017";
- input_function("success.php");
- //Getting last id from database
- function last_id()
- {
- mysqli_report(MYSQLI_REPORT_STRICT);
- try
- {
- $hostname = "localhost";
- $database = "paip_zam12017";
- $username = "paip_zam12017";
- $password = "zam12017";
- $conn = new mysqli($hostname, $username, $password, $database);
- $conn->set_charset("utf8");
- if ($conn->connect_errno!=0)
- {
- throw new Exception(mysqli_connect_errno());
- }
- else
- {
- $sql = "SELECT ID FROM ogloszenia ORDER BY ID DESC LIMIT 1";
- if($result = $conn->query($sql))
- {
- if($conn->connect_error)
- {
- throw new Exception("Connection failed");
- //die("Connection failed");
- }
- if ($result->num_rows >= 1)
- {
- $row = $result->fetch_assoc();
- $returning = $row['ID'];
- $result->free_result();
- }
- else
- {
- $returning = 0;
- }
- return $returning;
- exit();
- }
- else
- {
- throw new Exception($conn->error);
- }
- $conn->close();
- }
- }
- catch(Exception $e)
- {
- return -1;
- }
- //Returning last id from database
- }
- //Adding file
- function addfile($id, $plik)
- {
- // $patch = str_replace('input.script.php', '', $_SERVER['SCRIPT_FILENAME'])."media/$id/";
- // $f['type'] == 'image/png' or $f['type'] == 'image/jpeg' or $f['type'] == 'image/gif'
- ////XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- error_reporting(0);
- $pathdir="./media/".$id; // change your directory name
- mkdir("./media/".$id);
- if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif")))
- {
- if($_FILES["file"]["size"] < 4000000)
- {
- if ($_FILES["file"]["error"] > 0)
- {
- echo "Error: " . $_FILES["file"]["error"] . "<br />";
- return -3;
- }
- else
- {
- $path="media/".$id."/".$_FILES["file"]['name'];
- copy( $_FILES["file"]['tmp_name'], $path );
- return $path;
- #echo "<div class='table'><table><tr><Td>Upload:</td><td> " . $_FILES["file"]["name"] . "</td></tr>";
- #echo "<tr><td>Type:</td><td> " . $_FILES["file"]["type"] . "</td></tr>";
- #echo "<tr><td>Size: </td><td>" . ($_FILES["file"]["size"] / 1024) . " Kb</td></tr>";
- #echo "<tr><td>Stored in:</td><td> " .$pathdir."/".$_FILES["file"]["name"]."</td></tr></table></div>";
- }
- }
- else
- {
- #echo "Upload files less than 4000kb";
- return -1;
- }
- }
- else
- {
- #echo "Invalid file format";
- return -2;
- }
- //Returning path to file
- }
- //Adding data to database
- function adddata($id, $path)
- {
- mysqli_report(MYSQLI_REPORT_STRICT);
- try
- {
- $hostname = "localhost";
- $database = "paip_zam12017";
- $username = "paip_zam12017";
- $password = "zam12017";
- $conn = new mysqli($hostname, $username, $password, $database);
- $conn->set_charset("utf8");
- if ($conn->connect_errno!=0)
- {
- throw new Exception(mysqli_connect_errno());
- }
- else
- {
- $w1 = $_POST['imie']; #imie
- $w2 = $_POST['nazwisko']; #nazwisko
- $w3 = $_POST['telefon']; #telefon
- $w4 = $_POST['mail']; #Email
- $w5 = $_POST['opis']; #opis
- $w6 = $_POST['status']; #zaginiony czy znaleziony
- $w7 = $path; #Photo
- $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));
- if($result = $conn->query($sql))
- {
- if($conn->connect_error)
- {
- throw new Exception("Connection failed");
- //die("Connection failed");
- }
- return 0;
- exit();
- }
- else
- {
- throw new Exception($conn->error);
- }
- $conn->close();
- }
- }
- catch(Exception $e)
- {
- return -1;
- }
- //Returning error
- }
- //Main Function
- function input_function($location)
- {
- if (isset($_POST['submit']) == true)
- {
- if ((isset($hostname) && isset($database) && isset($username) && isset($password))) # D#1
- {
- echo "Błąd! (D#1)";
- exit();
- }
- $id = last_id();
- if ($id == -1)
- {
- echo "Błąd! (#3)";
- exit();
- }
- $id = $id + 1;
- $path = addfile($id, $_FILES['file']);
- if ($path == -2) # #2
- {
- echo "Błąd! (#2)";
- exit();
- }
- else if ($path == -1) # #5
- {
- echo "Błąd! (#5)";
- exit();
- }
- else if ($path == -3)# #6
- {
- echo "Błąd! (#6)";
- exit();
- }
- //$path = str_replace('/home/paip/domains/paip.com.pl/public_html/c/2017/1/app/', 'http://c.paip.com.pl/2017/1/app/', $path);
- $path = "http://vc.paip.com.pl/s/".$path;
- $err = adddata($id, $path);
- if ($err == -1) # #4
- {
- echo "Błąd! (#4)";
- exit();
- }
- header("Location: $location");
- exit();
- }
- else # #1
- {
- echo "Błąd! (#1)";
- exit();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement