Answers for "image upload in php mysql"

PHP
1

img upload in php

if(isset($_FILES['image']))
                {
                    $img_name = $_FILES['image']['name'];      //getting user uploaded name
                    $img_type = $_FILES['image']['type'];       //getting user uploaded img type
                    $tmp_name = $_FILES['image']['tmp_name'];   //this temporary name is used to save/move file in our folder.
                    
                    // let's explode image and get the last name(extension) like jpg, png
                    $img_explode = explode(".",$img_name);
                    $img_ext = end($img_explode);   //here we get the extension of an user uploaded img file

                    $extension= ['png','jpeg','jpg','gif']; //these are some valid img extension and we are store them in array.
Posted by: Guest on August-25-2021
0

image upload in php

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="image" />
         <input type="submit"/>
      </form>
      
   </body>
</html>
Posted by: Guest on May-08-2021
0

how to upload image in php and store in database and folder

// Get the name of images
  	$Get_image_name = $_FILES['image']['name'];
  	
  	// image Path
  	$image_Path = "images/".basename($Get_image_name);

  	$sql = "INSERT INTO student_table (imagename, contact) VALUES ('$Get_image_name', 'USA')";
  	
	// Run SQL query
  	mysqli_query($conn, $sql);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $image_Path)) {
  		echo "Your Image uploaded successfully";
  	}else{
  		echo  "Not Insert Image";
  	}
  }
Posted by: Guest on July-07-2020
0

Php mysql update form with image upload

<?php
include "header.php";

$banner_id = $_GET['id'];
$query = "SELECT * FROM banners WHERE id = $banner_id";
$q_result = mysqli_query($obj->conn, $query);
$res = mysqli_fetch_assoc($q_result);


if (isset($_POST['submit']))
{
    $title = $_POST['title'];
    $des = $_POST['discription'];

    $targetDir = "uploads/banners/";
    $fileName = basename($_FILES['banner']['name']);
    $targetFilePath = $targetDir . $fileName;
    $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
    move_uploaded_file($_FILES["banner"]["tmp_name"], $targetFilePath);

    if (!empty($fileName))
    {
        $sql = "UPDATE `banners` SET `title`='$title',`data`='$des',`image`='$fileName' WHERE `id`= '$banner_id'";
    }
    else
    {
        $sql = "UPDATE `banners` SET `title`='$title',`data`='$des' WHERE `id`= '$banner_id'";
    }

    $result = mysqli_query($obj->conn, $sql);

    if ($result == 'true')
    {
        echo "<script>alert('Banner updated successfully.')</script>";
        echo "<script>window.location = 'banners.php';</script>";
    }
    else
    {
        echo "<script>alert('Error')</script>";
    }
}

?>
Posted by: Guest on August-17-2021
0

image upload in php mysql

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 
  strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";

      $uploadOk = 1;
  } else {
    echo "File is not an image.";

      $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {

    echo "Sorry, file already exists.";
  $uploadOk = 0;
}


 // Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;

 }

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

      echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). 
  " has been uploaded.";

    } else {
    echo "Sorry, there was an error uploading your file.";

    }
}
?>
Posted by: Guest on September-14-2021
0

download image from mysql using php

//You can save this to test.php and call it with test.php?id=1 for example
<?php

//Database class with PDO (MySQL/MariaDB)
require_once("database.php"); //If you need this, write to [email protected] i'll send you the db class

//Connect to database
$database = new Database();
$pdo = $database->dbConnection();

//Get ID from GET (better POST but for easy debug...)
if (isset($_GET["id"])) {
	$id=(int)$_GET["id"];
} else {
  echo "Wrong input";
  exit;
}

//Prepare PDO SQL
$q = $pdo->prepare("SELECT * FROM `table_with_image` WHERE `id`=:p_id");

//Add some data
$q->bindparam(":p_id", $id); //Filter user input, no sanitize necessary here

//Do the db query
$q->execute();

//If something found (always only 1 record!)
if ($q->rowCount() == 1) {
  
  	//Get the content of the record into $row
	$row = $q->fetch(PDO::FETCH_ASSOC); //Everything with id=$id should be in record buffer
  	
  	//This is the image blob mysql item  
  	$image = $row['image'];
  	
  	//Clean disconnect
  	$database->disconnect();
    
  	//Now start the header, caution: do not output any other header or other data!
  	header("Content-type: image/jpeg");
    header('Content-Disposition: attachment; filename="table_with_image_image'.$id.'.jpg"');
    header("Content-Transfer-Encoding: binary"); 
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".strlen($image));
    //Output plain image from db
	echo $image;
} else {
  //Nothing found with that id, output some error
  $database->disconnect();
  echo "No image found";
}

//No output and exceution further this point
exit();
Posted by: Guest on November-05-2020

Browse Popular Code Answers by Language