I have made a code where you can choose an image from your computer and upload it to the database. For some reason, after I have uploaded it(it is uploaded in the database correctly), the image that is showned isnt the image I have uploaded but it is a little image showing that it cant get the image from the database. Can someone help me?? Here is the code:
index.php:
<?php
ob_start();
include_once('connect.php');
session_start();
?>
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php
//file properties
$file = $_FILES['image']['tmp_name'];
if(!isset($file)) {
echo 'Please select an image.';
}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 'That is not an image';
}else{
if (!$insert = mysqli_query($con,"INSERT INTO uploading_image(name,image) VALUES('$image_name','$image')")){
echo 'Problem uploading image';
}else{
$lastid = mysqli_insert_id();
echo 'Image uploaded. <br>Your image:<br><img src="get.php? id='.$lastid.'">';
}
}
}
?>
</body>
</html>
$get.php:
<?php
include_once('connect.php');
$id = addslashes($_REQUEST['id']);
$image = mysqli_query($con,"SELECT * FROM uploading_image WHERE id='$id'");
$find_image = mysqli_query($row = mysqli_fetch_array($image));
$image_db = $row['image'];
header("Content-type: image/png");
echo $image_db;
?>
check the datatype for the field image in the database. It should be blob or longblob. replace the following code
$find_image = mysqli_query($row = mysqli_fetch_array($image));
with this.
$find_image = mysqli_query($row = mysqli_fetch_assoc($image));
Related
I would like to display an image stored in my database but I keep getting that error.
The image is stored in my database as longblob.
I upload it using this piece of code in upload.php:
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="image" />
<input type="submit" name="submit" value="UPLOAD" />
</form>
<?php
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
$image = $_FILES['image']['tmp_name'];
$imgContent = addslashes(file_get_contents($image));
/*
* Insert image data into database
*/
//Insert image content into database
$insert = $pdo->query("UPDATE users SET image='".$imgContent."'"."WHERE id_user = ".$posts[0]->get_id());
if ($insert) {
echo "File uploaded successfully.";
} else {
echo "File upload failed, please try again.";
}
} else {
echo "Please select an image file to upload.";
}
}
?>
Then I try to display it in getImage.php:
<?php
$id = $_GET['id'];
$query = $pdo->prepare('SELECT * FROM users WHERE username LIKE :us');
$query->bindValue(':us', $_SESSION['login'], PDO::PARAM_STR);
$query->execute();
$user = $query->fetchAll(PDO::FETCH_CLASS, "User");
header ('Content-Type: image/png');
echo $user[0]->get_image();
?>
When I go however to /getImage.php?id=1, I have the error
The image cannot be displayed because it contains errors
What am I doing wrong?
Solved: I added ob_end_clean(); before the header and it worked.
fix my code please iam getting error : C:\xampp\tmp\php7A7B.tmp
i get this error while uploading image , can you fix it pls
otherwise: i want everyimage uploaded on this service add on my uploads folder
<html>
<head>
<title>imgfox upload images</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
image:
<input type="file" name="image"> <input type="submit" value="upload">
</form>
<?php
// connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("meinimages") or die(mysql_error());
// file properties
if(isset($_FILES['image'])){
echo $_FILES['image']['tmp_name'];
}
if (!isset($file))
echo "please choose a image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($image);
if ($image_size==FALSE)
echo "you try upload no/..image.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('','$image_name','$image')"))
echo "problem while upload $image.";
else
{
$lastid = mysql_insert_id();
echo "image is uploaded.<p />your image:<p /><img src=get.php?id=$lastid>";
}
}
}
I try upload image to mysql database and display it along with the description of image using php. After i upload the image and display it , a broken image was displayed but the description of the image was displayed without any error. How can I solve this problem ? Appreciate your help
<?php
$msg = "";
//if upload button is pressed
if(isset($_POST['upload']))
{
// the path to store the uploaded image
$target = "images/".basename($_FILES['image']['name']);
// connect to database
$db = mysqli_connect("localhost","root","","product");
// Get all the submitted data from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
mysqli_query($db,$sql); // stores the submitted data into the database table : product_list
// move uploaded image to the folder : image
if (move_uploaded_file($_FILES['image']['tmp_name'],$target))
{
$msg = "Image and text uploaded successfully";
}else
{
$msg = "There was a problem uploading image";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
$db = mysqli_connect("localhost","root","","product");
$sql = "SELECT * FROM product_list";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result))
{
echo "<div id='img_div'>";
echo "<img src='".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
?>
<form method="post" action="try.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>
This is my result :
You are storing it in the DB without the images directory. You either need to store it with that, or always remember to call it that way in your image calls.
echo "<img src='images/".$row['image']."'>";
or make your the record you are writing the same as the filesystem location.
$image = 'images/' . $_FILES['image']['name'];
Note you are open to SQL injections and file inclusion injections with this code.
try this
<?php
$msg = "";
//if upload button is pressed
if(isset($_POST['upload']))
{
// the path to store the uploaded image
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . basename( $_FILES["image"]["name"]);
// connect to database
$db = mysqli_connect("localhost","root","","product");
// Get all the submitted data from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
mysqli_query($db,$sql); // stores the submitted data into the database table : product_list
//#move_uploaded_file($_FILES['image']['tmp_name'], $target_path)
// move uploaded image to the folder : image
if (move_uploaded_file($_FILES['image']['tmp_name'],$target_path))
{
$msg = "Image and text uploaded successfully";
}else
{
$msg = "There was a problem uploading image";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
$db = mysqli_connect("localhost","root","","product");
$sql = "SELECT * FROM product_list";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result))
{
echo "<div id='img_div'>";
echo "<img src='".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
?>
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>
Can someone please help as I am at a loss. How can I get an image from MYSQL database onto my webpage. anyname.html
Database name is: upload
Table name is: images
id Primary int(11) AUTO_INCREMENT
name varchar(100)
size int(11)
type varchar(20)
content mediumblob
The upload works great but just cant get it to show the image of be able to use it in a html page. When I upload the image I get as a return: for number 11 image stored but I don't see an image: Here is my code, so please, please someone help as I have been at this for ages with no result.
my upload.php code:
<?php
// Check for post data.
if ($_POST && !empty($_FILES)) {
$formOk = true;
//Assign Variables
$path = $_FILES['image']['tmp_name'];
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
if ($_FILES['image']['error'] || !is_uploaded_file($path)) {
$formOk = false;
echo "Error: Error in uploading file. Please try again.";
}
//check file extension
if ($formOk && !in_array($type, array('image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'))) {
$formOk = false;
echo "Error: Unsupported file extension. Supported extensions are JPG / PNG.";
}
// check for file size.
if ($formOk && filesize($path) > 500000) {
$formOk = false;
echo "Error: File size must be less than 500 KB.";
}
if ($formOk) {
// read file contents
$content = file_get_contents($path);
//connect to mysql database
if ($conn = mysqli_connect('localhost', 'root', '', 'upload')) {
$content = mysqli_real_escape_string($conn, $content);
$sql = "insert into images (name, size, type, content) values ('{$name}', '{$size}', '{$type}', '{$content}')";
if (mysqli_query($conn, $sql)) {
$uploadOk = true;
$imageId = mysqli_insert_id($conn);
} else {
echo "Error: Could not save the data to mysql database. Please try again.";
}
mysqli_close($conn);
} else {
echo "Error: Could not connect to mysql database. Please try again.";
}
}
}
?>
<html>
<head>
<title>Upload image to mysql database.</title>
<style type="text/css">
img{
margin: .2em;
border: 1px solid #555;
padding: .2em;
vertical-align: top;
}
</style>
</head>
<body>
<?php if (!empty($uploadOk)): ?>
<div>
<h3>Your Image has been Uploaded:</h3>
</div>
<div>
<img src="image.php?id=<?=$imageId ?>" width="300px" height="300px"></img>
<strong><br /><br />Embed</strong>: <input size="30" value='<img src="image.php?id=<?=$imageId ?>">'>
</div>
<hr>
<?php endif; ?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" >
<div>
<h3>Image Upload:</h3>
</div>
<div>
<label>IMAGE SELECT<br /></label>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="image" />
<br /><br />
<input name="submit" type="submit" value="Upload Image">
</div>
</form>
</body>
</html>
It seems to be a duplicate from the issue there PHP display image BLOB from MySQL
Hope it helps.
If you store the image data in database, use the next image.php:
<?php
$id = $_GET['id'];
$link = mysql_connect('localhost', 'root', '');
mysql_select_db('upload');
$sql = "SELECT content FROM images WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['content'];
?>
<?php
if(isset($_POST['submit'])){
mysql_connect('localhost','root','');
$con_db=mysql_select_db('test');
if(!$con_db)echo"Error Connecting";
$imageName = mysql_real_escape_string($_FILES['image']['name']);
$imageData = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$imageType = mysql_real_escape_string($_FILES['image']['type']);
echo $imageName;
if(substr($imageType,0,5)=="image"){
mysql_query('insert into image values("","$imageData","imageName")');
}
else echo "Select Image to Post";
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table>
<td><tr>Upload image</tr><td><input type="file" name="image"></td>
<td><tr></tr><tr><input type="submit" name="submit"></tr></td>
</table>
I here have created a form and tried to upload the pic it got uploaded when i try to show the uploaded image its showing that there is a fatal error function must be a string
<?php
$my_con=mysql_connect('localhost','root','');
mysql_select_db('test');
$getid = $_GET('id');
if(isset($getid)){
$id = $getid;
$db_query = mysql_query("select * from 'image' where id='$id'");
while($row = mysql_fetch_row($db_query)){
$imageData = $row['image'];
}
header("content-type: image");
}
else{
echo "Error!";
}
?>
This is another php file to display the image