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.
Related
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-->
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>';
}
I´ve been working on this project for a quite while and, the other day I was doing some code when I walk by this piece of code that keeps giving me headaches. So i got my html and php code all right but whenever i try to upload an image to my database, the image goes null, what am i doing wrong?
<?php include "connection.php"; ?>
<?php
$n=$_POST["num"];
$t=$_POST["texto"];
$i=$_POST["imagem"];
$img = mysql_query("SELECT imagem2 from segurancaofensiva where nmr=$n");
$file = $_FILES['imagem']['tmp_name'];
$image = addslashes(file_get_contents($file));
$count = $connect->query("SELECT COUNT(DISTINCT nmr) FROM segurancaofensiva")->fetch_row()[0];
if ($connect->connect_error){
die("Connection failed: " . $connect->connect_error);
}
$sql = "UPDATE segurancaofensiva SET texto='$t', imagem='{$image}', imagem2='{$image}' where nmr=$n" ;
$sql1 = "UPDATE segurancaofensiva SET texto='$t', imagem='$img' where nmr=$n";
if ($_FILES['imagem']['name']!=='' && $connect->query($sql) !== false )
{
if ($n <= $count) {
echo "actualizou\n\n";
var_dump($n);
var_dump($i);
var_dump($image);
}
else
{
echo "nao atualizou numero fora dos limites";
}
} else {
if ($connect->query($sql1) !== false){
echo "atualizou\n\n";
} else {
echo "errp";
}
}
$connect->close();
?>
<?php include 'connection.php'; ?>
<?php
$campo = $_POST['selected'];
$query = "SELECT campo FROM segurancaofensiva";
$result1 = mysqli_query($connect, $query);
$stored = $campo;
$obterquery = "SELECT * FROM segurancaofensiva where campo ='$campo'";
$x = $connect->query ($obterquery) or die ("Erro na variavel resultado");
$final = $x->fetch_array (MYSQL_ASSOC);
?>
<html>
<body>
<head>
<link rel="stylesheet" type="text/css" href="css/styleBO.css">
</head>
<div class="formulario" style="width: 100%; height: 100%;">
<form name="form2" method="POST" action="">
<h6>Campo:</h6> <select name ="selected" id="selected" >
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
<?php endwhile;?>
</select>
<input type="submit" id="load" class="load" name="load" value="Carregar">
<input type="hidden" name="selectedValue" value="0"/><br>
</form>
</div>
<div class="formulario" id="form2" style="width: 100%; height: 100%;">
<form name="form1" target="apresenta" method="POST" action="menu3.php">
<label> Atualizar dados </label><br>
<h6>Texto:</h6><textarea name="texto" id="texto"><?php echo htmlspecialchars($final['texto']);?></textarea><br>
<h6>Imagem:</h6><input type="file" name="imagem"><br>
<input type="hidden" value="<?php echo htmlspecialchars($final['nmr']);?>" name="num">
<input type="submit" name="submit" value="Enviar" class="topo">
<input type="reset" value="Limpar" class="topo">
</form>
</div>
</body>
</html>
Change form html like below. PHP will not detect file object without this.
<form enctype="multipart/form-data">
Add form attribute as per your requirement.
You are missing enctype attribute in form tag
enctype='multipart/form-data'
If you post data and trying to upload file you must use enctype
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!
I have a simple webpage and a database to upload and show image on webpage.However, some image cannot be downloaded and shown correctly on webpage, some images show well. I have checked my database, each image can be inserted into database correctly.
My PHP code is below:
<?php
ini_set('mysql.connect_timeout',10);
ini_set('default_socket_timeout',5);
include_once 'dbconnect.php';
?>
<?php
if(isset($_POST['btn_Upload_Image']))
{
if(getimagesize($_FILES['image']['tmp_name']) == FALSE)
{
// echo "Please select an image.";
?>
<script>alert('Please select an image.');</script>
<?php
}
else
{
$image= addslashes($_FILES['image']['tmp_name']);
$Name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($Name,$image);
}
}
//displayimage();
function saveimage($Name,$Image)
{
$qry="Insert into Clothing (Picture_Name,Picture_Storage) values ( '$Name','$Image')";
// echo "$qry" ."</br>";
$result=mysql_query($qry);
if($result)
{
?>
<script>alert('Image uploaded.');</script>
<?php
}
else
{
?>
<script>alert('Image not uploaded.');</script>
<?php
}
}
function displayimage()
{
$qry = "Select * from Clothing";
$result=mysql_query($qry);
while($row = mysql_fetch_array($result))
{
echo '<img height="200" width="200" src="data:image;base64,'.$row[Picture_Storage].' "> ';
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello PeekABuy</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<div id="left">
<label>Cropped Images </label>
</div>
</div>
<div id="body">
<div id="Hotel-InfoForm">
<form method="post" enctype="multipart/form-data">
<center>
<table align="center" width="40%" border="0">
<tr>
<td>
<input type="file" name="image" />
<br/>
<br/>
<input type="submit" name="btn_Upload_Image" value="Upload" />
</td>
</tr>
<center>
</table>
<br>
<br>
<br>
<br>
<br>
<br>
</form>
<?php
displayimage();
?>
<br>
<br>
<br>
<br>
<hr>
</div>
</div>
</body>
</html>
1:How webpage looks like; 2: Datatype of my database; 3: Inserting status
Thanks a lot!