How To Upload An Image To Mysql
How to Insert image In MySQL Using PHP
In some registration php grade and application we need paradigm upload. Instance: For Profile motion-picture show upload, gallery photo upload, product image etc
There are two ways to insert images in mysql.
- Using binary format insert
- Using image upload in folder
Using binary format
In this blazon nosotros directly insert the image in mysql table using binary format.
Using image upload in folder
In this type nosotros upload the image in a folder and store the image name in MySql table.
For insert image in MySQL commencement we have to create a table in data base.
CREATE Tabular array `image` ( `id` int(eleven) NOT NULL, `paradigm` varchar(55) NOT Zero ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
index.php
<!DOCTYPE html> <html> <head> <title>Insert Paradigm in MySql using PHP</title> </caput> <body> <form activeness="upload.php" method="post" enctype="multipart/course-data"> <input type="file" name="image[]" /> <button type="submit">Upload</push> </form> </body> </html>
upload.php
<?php $cn=mysqli_connect("localhost","root","","image") or dice("Could not Connect My Sql"); $output_dir = "upload/";/* Path for file upload */ $RandomNum = fourth dimension(); $ImageName = str_replace(' ','-',strtolower($_FILES['image']['name'][0])); $ImageType = $_FILES['image']['type'][0]; $ImageExt = substr($ImageName, strrpos($ImageName, '.')); $ImageExt = str_replace('.','',$ImageExt); $ImageName = preg_replace("/\.[^.\due south]{iii,4}$/", "", $ImageName); $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt; $ret[$NewImageName]= $output_dir.$NewImageName; /* Try to create the directory if it does not be */ if (!file_exists($output_dir)) { @mkdir($output_dir, 0777); } move_uploaded_file($_FILES["image"]["tmp_name"][0],$output_dir."/".$NewImageName ); $sql = "INSERT INTO `img`(`paradigm`)VALUES ('$NewImageName')"; if (mysqli_query($cn, $sql)) { repeat "successfully !"; } else { echo "Fault: " . $sql . "" . mysqli_error($cn); } ?>
CREATE Tabular array `prototype` ( `id` int(xi) NOT Zippo, `paradigm` LONGBLOB NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here we using 1 file for insert prototype in MySQL:
- index.php HTML form that permit users to choose the image file they desire to upload.
Using binary format
<!DOCTYPE html> <html> <head> <title>Insert Epitome in MySql using PHP</title> </head> <body> <?php $msg = ''; if($_SERVER['REQUEST_METHOD']=='POST'){ $image = $_FILES['image']['tmp_name']; $img = file_get_contents($image); $con = mysqli_connect('localhost','root','','admin') or die('Unable To connect'); $sql = "insert into images (image) values(?)"; $stmt = mysqli_prepare($con,$sql); mysqli_stmt_bind_param($stmt, "south",$img); mysqli_stmt_execute($stmt); $bank check = mysqli_stmt_affected_rows($stmt); if($check==1){ $msg = 'Prototype Successfullly UPloaded'; }else{ $msg = 'Error uploading prototype'; } mysqli_close($con); } ?> <form activity="" method="post" enctype="multipart/form-data"> <input type="file" name="paradigm" /> <button>Upload</button> </form> <?php echo $msg; ?> </trunk> </html>
Source: https://www.studentstutorial.com/php/php-insert-image
Posted by: caseyunation.blogspot.com
0 Response to "How To Upload An Image To Mysql"
Post a Comment