Upload image php mysql

Code examples

4
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 IGNORE 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";
  	}
  }
1
0

php image upload in database

<?php
error_reporting(0);
?>
<?php
$msg = "";

// If upload button is clicked ...
if (isset($_POST['upload'])) {

	$filename = $_FILES["uploadfile"]["name"];
	$tempname = $_FILES["uploadfile"]["tmp_name"];
		$folder = "image/".$filename;

	$db = mysqli_connect("localhost", "root", "", "image_upload");

		// Get all the submitted data from the form
		$sql = "INSERT IGNORE INTO images (filename) VALUES ('$filename')";

		// Execute query
		mysqli_query($db, $sql);

		// Now let's move the uploaded image into the folder: image
		if (move_uploaded_file($tempname, $folder)) {
			$msg = "Image uploaded successfully";
		}else{
			$msg = "Failed to upload image";
	}
}
$result = mysqli_query($db, "SELECT * FROM images");
?>

<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
<link rel="stylesheet" type= "text/css" href ="style.css"/>
<div id="content">

<form method="POST" action="" enctype="multipart/form-data">
	<input type="file" name="uploadfile" value=""/>

	<div>
		<button type="submit" name="upload">UPLOAD</button>
		</div>
</form>
</div>
</body>
</html>
0
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();
0
0

how to print image just on side where upload php

<?php    $galleryPath = "Gallery/";        $descriptions = new DOMDocument("1.0");    $descriptions->appendChild($descriptions->createElement("files"));        while (($uploadedFile = UploadedFiles::fetchNext()) != null){        $sourceFile = $uploadedFile->getSourceFile();        $sourceFileName = $sourceFile->getFileName();            $descriptionsFile = $descriptions->createElement("file");        $descriptionsFile->setAttribute("name", $sourceFileName);        $descriptionsFile->setAttribute("width", $sourceFile->getWidth());        $descriptionsFile->setAttribute("height", $sourceFile->getHeight());        $descriptionsFile->setAttribute("description", $uploadedFile->getDescription());        $descriptions->documentElement->appendChild($descriptionsFile);                    $sourceFile->save($galleryPath. "/" .$sourceFileName);    }            $descriptions->save("Descriptions.xml");?>

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................