1
2 if(isset($_FILES['image']))
3 {
4 $img_name = $_FILES['image']['name']; //getting user uploaded name
5 $img_type = $_FILES['image']['type']; //getting user uploaded img type
6 $tmp_name = $_FILES['image']['tmp_name']; //this temporary name is used to save/move file in our folder.
7
8 // let's explode image and get the last name(extension) like jpg, png
9 $img_explode = explode(".",$img_name);
10 $img_ext = end($img_explode); //here we get the extension of an user uploaded img file
11
12 $extension= ['png','jpeg','jpg','gif']; //these are some valid img extension and we are store them in array.
13
1<?php
2 if(isset($_FILES['image'])){
3 $errors= array();
4 $file_name = $_FILES['image']['name'];
5 $file_size =$_FILES['image']['size'];
6 $file_tmp =$_FILES['image']['tmp_name'];
7 $file_type=$_FILES['image']['type'];
8 $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
9
10 $extensions= array("jpeg","jpg","png");
11
12 if(in_array($file_ext,$extensions)=== false){
13 $errors[]="extension not allowed, please choose a JPEG or PNG file.";
14 }
15
16 if($file_size > 2097152){
17 $errors[]='File size must be excately 2 MB';
18 }
19
20 if(empty($errors)==true){
21 move_uploaded_file($file_tmp,"images/".$file_name);
22 echo "Success";
23 }else{
24 print_r($errors);
25 }
26 }
27?>
28<html>
29 <body>
30
31 <form action="" method="POST" enctype="multipart/form-data">
32 <input type="file" name="image" />
33 <input type="submit"/>
34 </form>
35
36 </body>
37</html>
1// Get the name of images
2 $Get_image_name = $_FILES['image']['name'];
3
4 // image Path
5 $image_Path = "images/".basename($Get_image_name);
6
7 $sql = "INSERT INTO student_table (imagename, contact) VALUES ('$Get_image_name', 'USA')";
8
9 // Run SQL query
10 mysqli_query($conn, $sql);
11
12 if (move_uploaded_file($_FILES['image']['tmp_name'], $image_Path)) {
13 echo "Your Image uploaded successfully";
14 }else{
15 echo "Not Insert Image";
16 }
17 }
1<?php
2$target_dir = "uploads/";
3$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
4$uploadOk = 1;
5$imageFileType =
6 strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
7
8// Check if image file is a actual image or fake image
9if(isset($_POST["submit"])) {
10 $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
11 if($check !== false) {
12 echo "File is an image - " . $check["mime"] . ".";
13
14 $uploadOk = 1;
15 } else {
16 echo "File is not an image.";
17
18 $uploadOk = 0;
19 }
20}
21
22// Check if file already exists
23if (file_exists($target_file)) {
24
25 echo "Sorry, file already exists.";
26 $uploadOk = 0;
27}
28
29
30 // Check file size
31if ($_FILES["fileToUpload"]["size"] > 500000) {
32 echo "Sorry, your file is too large.";
33 $uploadOk = 0;
34
35 }
36
37// Allow certain file formats
38if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
39&& $imageFileType != "gif" ) {
40 echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
41 $uploadOk = 0;
42}
43
44// Check if $uploadOk is set to 0 by an error
45if ($uploadOk == 0) {
46 echo "Sorry, your file was not uploaded.";
47// if everything is ok, try to upload file
48} else {
49
50 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
51
52 echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])).
53 " has been uploaded.";
54
55 } else {
56 echo "Sorry, there was an error uploading your file.";
57
58 }
59}
60?>
1<?php
2include "header.php";
3
4$banner_id = $_GET['id'];
5$query = "SELECT * FROM banners WHERE id = $banner_id";
6$q_result = mysqli_query($obj->conn, $query);
7$res = mysqli_fetch_assoc($q_result);
8
9
10if (isset($_POST['submit']))
11{
12 $title = $_POST['title'];
13 $des = $_POST['discription'];
14
15 $targetDir = "uploads/banners/";
16 $fileName = basename($_FILES['banner']['name']);
17 $targetFilePath = $targetDir . $fileName;
18 $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
19 move_uploaded_file($_FILES["banner"]["tmp_name"], $targetFilePath);
20
21 if (!empty($fileName))
22 {
23 $sql = "UPDATE `banners` SET `title`='$title',`data`='$des',`image`='$fileName' WHERE `id`= '$banner_id'";
24 }
25 else
26 {
27 $sql = "UPDATE `banners` SET `title`='$title',`data`='$des' WHERE `id`= '$banner_id'";
28 }
29
30 $result = mysqli_query($obj->conn, $sql);
31
32 if ($result == 'true')
33 {
34 echo "<script>alert('Banner updated successfully.')</script>";
35 echo "<script>window.location = 'banners.php';</script>";
36 }
37 else
38 {
39 echo "<script>alert('Error')</script>";
40 }
41}
42
43?>
44
1//You can save this to test.php and call it with test.php?id=1 for example
2<?php
3
4//Database class with PDO (MySQL/MariaDB)
5require_once("database.php"); //If you need this, write to office@predl.cc i'll send you the db class
6
7//Connect to database
8$database = new Database();
9$pdo = $database->dbConnection();
10
11//Get ID from GET (better POST but for easy debug...)
12if (isset($_GET["id"])) {
13 $id=(int)$_GET["id"];
14} else {
15 echo "Wrong input";
16 exit;
17}
18
19//Prepare PDO SQL
20$q = $pdo->prepare("SELECT * FROM `table_with_image` WHERE `id`=:p_id");
21
22//Add some data
23$q->bindparam(":p_id", $id); //Filter user input, no sanitize necessary here
24
25//Do the db query
26$q->execute();
27
28//If something found (always only 1 record!)
29if ($q->rowCount() == 1) {
30
31 //Get the content of the record into $row
32 $row = $q->fetch(PDO::FETCH_ASSOC); //Everything with id=$id should be in record buffer
33
34 //This is the image blob mysql item
35 $image = $row['image'];
36
37 //Clean disconnect
38 $database->disconnect();
39
40 //Now start the header, caution: do not output any other header or other data!
41 header("Content-type: image/jpeg");
42 header('Content-Disposition: attachment; filename="table_with_image_image'.$id.'.jpg"');
43 header("Content-Transfer-Encoding: binary");
44 header('Expires: 0');
45 header('Pragma: no-cache');
46 header("Content-Length: ".strlen($image));
47 //Output plain image from db
48 echo $image;
49} else {
50 //Nothing found with that id, output some error
51 $database->disconnect();
52 echo "No image found";
53}
54
55//No output and exceution further this point
56exit();