Ok so i uploaded images with code below but i dont know how to display it, i want to make gallery so all i want is to display all images on one page, if you could explain that would be helpfull too!
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phplogin";
$connect = mysqli_connect($servername,$username,$password,$dbname);
$file = $_FILES['image']['tmp_name'];
$ime = $_POST['ime'];
if(!isset($file))
{
echo "Izaberite sliku";
}
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
{
echo "Niste izabrali dobru sliku";
}
else
{
if(!$insert = mysqli_query($connect,"INSERT INTO store VALUES ('','$image_name','$image','$ime')"))
{
echo "Problem sa postavljanjem slike";
}
else
{
//$lastid = mysqli_insert_id($connect);
// I WANT TO DISPLAY IMAGES HERE
//echo "Image uploaded.<p />Slika:<p /><img src=get.php>";
}
}
}
?>
This code is to get image but it only display last ID and i dont know how to make it display all images
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
Save images as files instead of blobs. Store url to image in db as varchar
The html
<form role="form" class="form-inline" enctype="multipart/form-data" method="post" action=""> <div class="form-group"> <input type="file" class="form-control" name="image"/> </div>
<input type="submit" name="upload" class="btn btn-success" value="Upload" /> </form>
php
define('UPLOAD_PATH','images/');
This is the constant for the path of the images. In this case its a folder named images in the same directory as the script you are working on
$errors = array();
function output_errors($errors){
$output=array();
foreach($errors as $error){
$output[]='<li>'.$error.'</li>';
}
return '<ul>'.implode('',$output).'</ul>';
}
if(isset($_POST['upload'])){
if(!empty($_FILES['image']['name'])){
$image=$_FILES['image']['name'];
$image_type=$_FILES['image']['type'];
$image_size=$_FILES['image']['size'];
if((($image_type=='image/gif') || ($image_type=='image/jpeg') || ($image_type=='image/png') || ($image_type=='image/pjpeg')) &&
($image_size>0) /*&& ($image_size<=MAX_FILE_SIZE)*/){
if($_FILES['image']['error']==0){
/*give each image a unique name*/
$image=microtime().$image;
/*move uploaded file to permanent folder*/
$target=UPLOAD_PATH.$image;
if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){
if(mysqli_query($connect,"INSERT INTO images(`image`) VALUES ('$image')") ){
$message="Image was uploaded sucessfully";
}else{
$errors[]="Error,image was not uploaded successfully";
/*delete permanent file from server*/
@unlink(UPLOAD_PATH.$image);
}
}/*end of move uploaded file*/
}
}else{
$errors[]="File uploaded must be of type png, jpeg or gif";
/*delete temporary image file*/
@unlink($_FILES['image']['tmp_name']);
}/*end of image validation*/
}else{
$errors[]="Please select an image file";
}/*empty*/
}
if(!empty($errors))echo output_errors($errors);
if(!empty($message)) echo $message;
Your images table could appear like this
CREATE TABLE IF NOT EXISTS `images` (
`image_id` int(11) NOT NULL,
`image` varchar(255) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;
To get an image from db, you could have a query that selects image
from table images. To display image in html do
$queryImage=mysqli_query($connect,"SELECT `image` FROM images");
while($rowImage=mysqli_fetch_assoc($queryImage)){?>
<img src="<?php echo UPLOAD_PATH.$rowImage['image'] ;?>" style="width:250px; height:200px"/>
<?php
}/*end of while loop getting images*/?>