I have a problem in displaying images from database to page - php

I have created a website with an upload feature for gallery.
The upload works flawless,but it doesn't shows the images on the webpage.
I can not find the error in the code
I have tried to display the image as a background-image for the box I created and with an image tag with the attribute src.
The last method shows me that the website is reading the image but it can not be displayed.
<div class="wrapper">
<h2>Art Gallery</h2>
<div class="Gallery-container">
<?php
include_once 'includes/dbh.inc.php';
$sql = "SELECT * FROM gallery1 ORDER BY orderGallery DESC";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed!";
} else{
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
echo '<a href="#">
<div><img src=url(gallery/img/'.$row["imgfullnameGallery"].');></div>
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</a>';
}
}
?>
</div>
<?php
if (isset($_SESSION['username'])) {
echo '<div class="gallery-upload">
<form action="includes/galleryupload.inc.php" method="post" enctype="multipart/form-data">
<input type="text" name="filename" placeholder="File name...">
<input type="text" name="filetitle" placeholder="Title...">
<input type="text" name="filedesc" placeholder="Description...">
<input type="file" name="file">
<button type="submit" name="submit">UPLOAD</button>
</form>
</div>';
}
?>
</div>

You have used the wrong syntax img tag
please update below code
while ($row = mysqli_fetch_assoc($result)) {
echo '<a href="#">
<div><img src="gallery/img/'.$row["imgfullnameGallery"].'"></div>
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</a>';
}

Related

Can't post image file into given directory with $_FILES function

I have two pretty much similar cases of adding image files to directory and they both work, but I don't know why this one doesn't, since it's done on the same principles.
And since it's not pasting into the "show" directory, the output is following:
add-album.php
<?php
include "config.php";
if (isset($_POST["submit_album"])) {
$album = $_POST["album_name"];
$album_prikazna = $_POST["album_prikazna"];
$photo = $_FILES["album_prikazna"]["album_name"];
$upload = "show/".$photo;
$add_album = $conn->query("INSERT INTO gallery_albums (album_name, album_prikazna) VALUES ('$album', '$album_prikazna')");
move_uploaded_file($_FILES['album_prikazna']['album_name'], $upload);
header("Location: fotogalerije.php?add_album_action=successfull");
}
?>
fotogalerije.php
<div class="container">
<!--Images-->
<div class="">
<h3>Arhivi</h3>
<form method="post" action="add-album.php">
<label>Dodaj novi album:</label><br>
<input type="text" name="album_name" /> <input type="submit" name="submit_album" value="Add" /><br>
<input type="file" class="mt-3" name="album_prikazna">
</form><br>
<?php
if (isset($_GET["add_album_action"])) {
if ($_GET["add_album_action"] == "successfull") { ?>
Nov album dodan!<br><br>
<?php }
}
?>
<div class="">
<?php
$albums = $conn->query("SELECT * FROM gallery_albums");
while ($album_data = $albums->fetch_assoc()) {
$photos = $conn->query("SELECT * FROM gallery_photos WHERE album_id = ".$album_data["album_id"]."");
?>
<div class="p-0 m-0">
<img src="<?php echo $album_data['album_prikazna']; ?>" width="125" class="p-1" />
<a href="view-album.php?album_id=<?php echo $album_data["album_id"] ?>">
<?php echo $album_data["album_name"];
$imagePrikazna = $album_data["album_prikazna"];
?>
(<?php echo $photos->num_rows; ?>)</a>
<?php }
?>
</div>
</div>
</div>
</div>
<!--!Images-->

Default image won't show when database is still empty

Basically, the whole thought here is changing profile pictures and it will be uploaded in the database.
I want to show a default icon first when the database is still empty but my codes just displays a broken image thumbnail. What could have gone wrong?
<form method="post" enctype="multipart/form-data">
<div class="row form-group">
<div class="col col-md-3">
<label for="text-input" class="form-control-label">Profile Picture</label>
</div>
<div class="col-9 col-md-9">
<div class="input-group">
<?php
$query = "SELECT image FROM member WHERE member_id = '$ID'";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result))
{
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'">';
}
} else{
echo "<img src='images/icon.jpg' style='height:200px'draggable='false'>";
}
?>
<div class="col-12">
<input type="file" name="image" id="image" style ='margin-left:-10px;margin-top:5px'>
</div>
<div class="col-12">
<input type="submit" name="insert" id="insert" value="Change Profile Picture" class="btn btn-primary" style ='margin-left:-10px;margin-top:5px'>
</div>
<?php
if(isset($_POST["insert"])){
$file = addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$query = "UPDATE member SET image = '$file' WHERE member_id='$ID'";
if(mysqli_query($con, $query)){
echo ("<script LANGUAGE='JavaScript'>
window.alert('Profile Picture has been successfully changed!');
window.location.href='edit.php';
</script>");
}
}
?>
</div>
</div>
</div>
</form>
if your database is empty upload query will not work. if your image row is empty then your code should be like if image row is not empty display image else display the default image.
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_array($result)) {
if (isset($row['image']) && !empty($row['image'])) {
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'">';
} else {
echo "<img src='images/icon.jpg' style='height:200px'draggable='false'>";
}
} // endwhile
} else{
echo "no result found";
}
While writing HTML or CSS code inside PHP you should put a \ before all double quotations and you need change all quotations to double quotation.like shown below:
echo "<img src=\"images/icon.jpg\" style=\"height:200px;\" draggable=\"false\">";
I didn't check your PHP codes, If you've coded them properly the problem is obviously what I mentioned.

Picture does not appear after successfully uploading it

so today I was trying to upload photo from admin file to user file. One thing I discovered is that my code works fine, but something is a little bit wrong I don't know where. When I successfully uploaded the picture, the picture didn't appear. But it successfully been uploaded. Could you help me out?
This is the code I used to upload the picture
<?php
require_once ('db/database.php');
if(isset($_POST['submit']))
{
$name = basename($_FILES['file_upload']['name']);
$t_name = $_FILES['file_upload']['tmp_name'];
$dir = 'fotovid';
$kat = $_POST['kat'];
if(move_uploaded_file($t_name, $dir."/".$name))
{
mysqli_select_db($koneksi, 'koneksi_oop');
$query = "INSERT INTO gallery (id_gambar, kat_gambar, nama_gambar, path) VALUES ('', $kat, '$name', 'fotovid/$name')";
$res = mysqli_query($koneksi, $query);
echo "Berhasil upload foto";
} else {
echo "Gagal upload foto";
}
}
?>
<div class="wrapper">
<div class="panel"">
<div align="center" style="padding-top: 100px;">
<!-- <div class="container1" style="background-color: none;margin-bottom: 235px;">
<label for="file-input">Upload Video</label>
<input type="file" accept=".mp4,.mkv" id = "file-input" style="background-color: none; width: 300px; "><br/>
<script type="text/javascript" src = "assets/js/videoJS.js"></script>
</div> -->
<form action="inputfoto.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" /><br/>
<label>Kategori</label>
<input type="text" name="kat"><br/>
<input type="submit" name="submit" value="Upload">
</form>
</div>
</div>
</div>
While this is the class where I call 2 categories from database, which is 'Foto' and 'Video'.
<?php
include ('admin/db/database.php');
$query = "SELECT * FROM kategori_gambar";
$res = mysqli_query($koneksi, $query);
while ($row=mysqli_fetch_array($res))
{
?>
<div>
<fieldset style="margin:0px 40px 100px 40px;">
<legend>
<a href="keluar_gambar.php?kat_gambar=<?= $row['id'];?>">
<?php
echo $row['nama'];
?>
</a>
</legend>
</fieldset>
</div>
<?php
}
?>
And the last one is where the output should comes out.
$query = "SELECT * FROM gallery WHERE kat_gambar =".$_GET['kat_gambar'];
$res = mysqli_query($koneksi, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
while($row=mysqli_fetch_array($res))
{
?>
<img src = "<?php echo $row['path']; ?>" width='300px' height= "200px" /><br/>
<?php
}
?>
This is what happened when I tried to insert the image
enter image description here
enter image description here
enter image description here
enter image description here
I think there is a permission issue. could you please show us the output of the following code.
$row=mysqli_fetch_array($res)
and also check the folder where you tried to upload.

Can't upload file in BLOB to the database, Directory issues in PHP

I'm having difficulties to insert the value as blob type always empty and the other method can't insert the images in a folder. Please you can show the way out. Thanks! This code is to save the image in a folder and then access it with the name to display, but is not saving the photos to the folder. I edited and include the form here, containing JavaScript and css .It looks some how messy but i'm a beginner. Thanks.
<?php
include("../views/post_form.php");
require("../includes/include.php");
require("../includes/sess_n.php");
if ($_SERVER["REQUEST_METHOD"]== "POST")
{
$usertext = $_POST["usertext"];
$image = $_FILES['image']['name'];
$talent =$_POST["talenttype"];
if(empty($usertext) || empty($image)|| empty($talent))
{
die();
}
$id = $_SESSION["id"];
$folder ="images/".basename($_FILES['image']['name']);
move_uploaded_file($_FILES['image']['tmp_name'],$folder) ;
$query = mysqli_query($link,"SELECT * FROM `upload` WHERE id = '$id'");
$upload = mysqli_query($link,"INSERT INTO `upload` (description,image,talent,folder) VALUES('$usertext','$image','$talent','$folder ')");
}
?>
To display, I want the photos to be save to the folder, not saving. I used blob method not inserting into the database.
<?php
include("../views/feeds_form.php");
require("../includes/include.php");
require("../includes/sess_n.php");
$query = mysqli_query($link,"SELECT * FROM `upload` ");
if ($query === false)
{
die();
}
echo '<table>';
while ($run = mysqli_fetch_assoc($query))
{
echo '<tr>';
echo '<td>';?> <img src =" <?php echo $run["image"]; ?>" height=100 width=100> <?php echo '<td>';
echo '<td>'; echo $run["description"]; echo '<td>'; echo $run["folder"];echo '<td>';
echo '</tr>';
}
echo '</table>';
?>
The form here.
<?php
include("../public/header.php");
?>
<div><title>post</title></div>
<style>
.form-inline
{
text-align: center;
margin-bottom: 50px;
}
</style>
<script type="text/javascript">
function validate(){
var usertext = document.getElementById("usertext");
var talent = document.getElementById("talenttype");
var image = document.getElementById("image");
if (usertext.value === "" && talent.value === "" && image.value ==="")
{
alert("Field Must Not be Empty");
}
}
</script>
<form class="form-inline" method ="POST" action ="post.php" enctype="multipart/form-data" onsubmit= "return validate();">
<div class="form-group">
<label class="sr-only" for="exampleInputEmail3"> </label>
<textarea class="form-control" id = "usertext" name ="usertext" rows="5" placeholder="Describe Person Here"></textarea> <br><hr>
<label class="sr-only" for="exampleInputEmail3"></label><br>
<div class="form-group">
<label for="exampleInputPassword1"></label>
<input type="file" class="form-control" id = "image" id="exampleInputPassword1" name="image" placeholder="image">
</div> <br><br><br> <hr>
<div class="form-group">
<label for="exampleInputPassword1"></label>
<input type="text" class="form-control" id = "talenttype" id="exampleInputPassword1" name = "talenttype" placeholder="Talent-Type"><br><br>
</div> <hr>
<div>
<button type="submit" name ="post" class="btn btn-default">Post</button><br>
</div>
</div>
</form>
<?php
include("../public/footer.php");
?>
I was having permission issues, the images folder permission was changed. That solves everything. Thanks!

Form with textarea and multiple files input option repeats the entry while uploading to database

I've form with two Text areas and a multiple file input field and I need to upload them to database ....
here is my HTML code
<form method="post" action="../php/new-upload.php" enctype="multipart/form-data">
<div id="tabs-1">
<textarea placeholder="Hi" name="n_head" class="n_head"></textarea>
<textarea placeholder="Story..." name="n_st" class="n_st"></textarea>
<div class="tg-it">
<div class="tg-it-hd">Add Tag:</div>
<input name="tags" id="singleFieldTags2" value="Election, Namo">
</div>
</div>
<div id="tabs-2" class="new-post-det">
<div id="upload">
<div id="drop">
Drop Images Here
or
<a>Browse</a>
<input type="file" name="upl[]" multiple />
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
</div>
</div>
<div id="new-ps-bt">
<input type="submit" class="new-ps-bt" value="Post" />
<a id="bk-bt" class="bk-mn" href="../">Back <</a>
</div>
</form>
and here is my new-upload.php file
<?php
include("../config.php");
session_start();
$n_head=$_POST['n_head'];
$n_story=$_POST['n_story'];
$n_up=$_SESSION["user_id"];
$query = "INSERT INTO news(N_headline,N_story,N_U_id)VALUES('$n_head','$n_story','$n_up')";
$data = mysql_query ($query)or die(mysql_error());
$last_id = mysql_insert_id();
foreach ($_FILES['upl']['name'] as $filename) {
$n_img = $_FILES['upl']['tmp_name'];
$query_1 = "INSERT INTO news_img(N_id,ng_img)VALUES('$last_id','$n_img')";
$data_2 = mysql_query ($query_1)or die(mysql_error());
}
if($data)
{
header("Location:../");
}
mysql_close($con);
?>
so the problem is when this form is submitted, the database entry gets repeated the number of times same as the number of photos selected ....
help me out with this problem...
Please change these code in respective place
$n_story = $_POST['n_story'];
foreach ($_FILES['upl']['name'] as $filename) {
$n_img = $filename;
$query_1 = "INSERT INTO news_img(N_id,ng_img)VALUES('$last_id','$n_img')";
$data_2 = mysql_query ($query_1)or die(mysql_error());
}
Hope that fix your problem.

Categories