Error saving .pdf files into MySQL as BLOBs using PHP - php

I did a small form in HTML that allows you to add files and records everything in a database.
Everything works correctly when I save images, but when I try to save the PDF file type doesn't work.
Does anyone could tell me where I am failing?
Thank you all.
Code of Form
<tr><td>File:</td><td>
<input type="file" name="image" accept="image/jpeg, image/png, application/pdf" maxlength="200" title="Choose File" >
</td></tr>
PHP code to save in database
$image= addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
$image_name = addslashes($_FILES['image']['tmp_name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "";
else{
$insert = "INSERT INTO table(name, date, image)
VALUES ('$name', curdate(), '$image')";
mysql_query($insert);
}

getimagesize() returns FALSE for non image files (eg pdf). Your insert was never executed.

Related

how to upload picture and store in database

$image = file_get_contents($_FILES['image']['tmp_name']);
$image = mysql_real_escape_string($image);
mysql_query("UPDATE ngc set pic='" . $image "' WHERE username='" . $_SESSION["username"] . "'");
<form method="post" action="" enctype="multipart/form-data">
Upload Image :<input type="file" name="image" id="file">
<br><input type="submit" name="submit" value="Update!" class="btnSubmit">
</form>
i want to upload image to database..
Read this very nice example here: http://www.mysqltutorial.org/php-mysql-blob/
Also see this image gallery example here: http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml
BLOBs are data types in MySql database which can help you store image files in database directly.
Although a better way would be to store the file on the disk and store the path of that variable in the database.
get the Content of the file and save in database;
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
Mysql:
$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";
First of all if you want to save the entire image into the database , you must have the pic attribute type set to BLOB , here you have two options , either save the entire image into database , or save the name after uploading the images into a specified folder with a unique name each , so you can retrieve the images by name into this directory.

Blob datatype Image not stored in database

I am trying to store the image in database using the blob datatype.
but my program was not storing the image in database.
code:
form.php:
<form action="upload.php" method="post" enctype="multipart/form-data">
File Name<input type="file" name="image" /><br />
<input type="submit" value="Upload" />
</form>
upload.php:
<?php
require_once('connection.php');
if(isset($_POST['submit'])){
$image = addslashes(file_get_contents($_FILES[image]['tmp_name']));
$query = "INSERT INTO images ('image') VALUES('".$image."')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();
echo "Image id is ".mysql_insert_id();
}
?>
please resolve my problem..
A BLOB can store 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes.
Look at Storage Requirements for String Types.
My suggestion is use LONGBLOB instead of BLOB.
Hope it will works.
try to use the below code.
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$query = "INSERT INTO images (image) VALUES('".$image."')";
i think this time its work fine...
Try using backticks on fieldName
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
try this one.this is also another approach for storing the blob image.
code:
$image= $_FILES["image"];
$image= mysql_real_escape_string("$image");
$query = "INSERT INTO images (image) VALUES('".$image."')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();

How to upload images into MySQL database using PHP code

I am trying to save images in my database from HTML form. I have written PHP code to accomplish this task. The program is not generating any error message, but also not inserting image data in MySQL database. Kindly check it.
Here i am sharing a excerpt from my code.
/*-------------------
IMAGE QUERY
---------------*/
$file =$_FILES['image']['tmp_name'];
if(!isset($file))
{
echo 'Please select an Image';
}
else
{
$image_check = getimagesize($_FILES['image']['tmp_name']);
if($image_check==false)
{
echo 'Not a Valid Image';
}
else
{
$image = file_get_contents ($_FILES['image']['tmp_name']);
$image_name = $_FILES['image']['name'];
if ($image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )"))
{
echo $current_id;
//echo 'Successfull';
}
else
{
echo mysql_error();
}
}
}
/*-----------------
IMAGE QUERY END
---------------------*/
<form action='insert_product.php' method='POST' enctype='multipart/form-data' ></br>
File : <input type='file' name= 'image' >
</form>
Error Message
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 1
Firstly, you should check if your image column is BLOB type!
I don't know anything about your SQL table, but if I'll try to make my own as an example.
We got fields id (int), image (blob) and image_name (varchar(64)).
So the code should look like this (assume ID is always '1' and let's use this mysql_query):
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
echo "Something went wrong! :(";
}
You are doing it wrong in many ways. Don't use mysql functions - they are deprecated! Use PDO or MySQLi. You should also think about storing files locations on disk. Using MySQL for storing images is thought to be Bad Idea™. Handling SQL table with big data like images can be problematic.
Also your HTML form is out of standards. It should look like this:
<form action="insert_product.php" method="POST" enctype="multipart/form-data">
<label>File: </label><input type="file" name="image" />
<input type="submit" />
</form>
Sidenote:
When dealing with files and storing them as a BLOB, the data must be escaped using mysql_real_escape_string(), otherwise it will result in a syntax error.
Just few more details:
Add mysql field
`image` blob
Get data from image
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
Insert image data into db
$sql = "INSERT INTO `product_images` (`id`, `image`) VALUES ('1', '{$image}')";
Show image to the web
<img src="data:image/png;base64,'.base64_encode($row['image']).'">
End
This is the perfect code for uploading and displaying image through MySQL database.
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo " error ";
}
else
{
$image = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($image));
saveimage($image);
}
}
function saveimage($image)
{
$dbcon=mysqli_connect('localhost','root','','dbname');
$qry="insert into tablename (name) values ('$image')";
$result=mysqli_query($dbcon,$qry);
if($result)
{
echo " <br/>Image uploaded.";
header('location:urlofpage.php');
}
else
{
echo " error ";
}
}
?>
</body>
</html>

Php mysql registration with image

Okay I know I just asked a question about this but now I have it uploading the image to the data base and tried to re do some things to get the text to go in there I added everything back in for the first name but it dosnt seem to be making its way back any help would be amazing thank you btw this community is awesome.
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="UploadContent.php" method="POST" enctype="multipart/form-data">
<p>FirstName:</p><input type="FirstName" name"FirstName"/>
<p>File:</p><input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php
// connect to database
include"config.php";
// file properties
$FirstName = $_POST['FirstName'];
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select a profile pic";
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 isn't a image.";
else
{
$insert = mysql_query("INSERT INTO content VALUES ('".$FirstName."','','','','','','','','','$image_name','$image')");
}
}
?>
</body>
</html>
I changed my insert code as follows
$insert = mysql_query("INSERT INTO content (FirstName, LastName, Gender, City, State, BirthMonth, BirthDay, BirthYear, ProfileUrl, ProfilePicName, ProfilePic) VALUES ('".$FirstName."','".$LastName."','".$Gender."','".$City."','".$State."','".$BirthMonth."','".$BirthDay."','".$BirthYear."','".$ProfielUrl."','$image_name','$image')");
because I was trying to get this youtube video to help but still no luck
http://www.youtube.com/watch?v=xAkunfiZwYQ
Well, whether you get your answer or not but i am not agree with your approach of storing images into database, you may (not may have to) store them in a file system, and the path in database, that will be the correct way to do. here is an good stack post to read Storing images

How to save image in MYSQL database without post method?

I want to save gif type image in mySQL database without post method.
<form action="add_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploaded_file"><br>
<input type="submit" value="Upload file">
</form>
I want to upload file by php code only. for example:
// Gather all required data
$name = $File_Name;
$mime = $File_Type;
$data = $File_Data;
$size = $File_Size;
// Create the SQL query
$query = "
INSERT INTO `file` (
`name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
I have more then 21,0000 image in my local. I want to save image in loop.
Upload the files using FTP, then write a little PHP script that you run only once
that loops through the folder and adds the images to the database.
Make sure to remove the php file after it is done, so that you don't run it again accidentally ;)

Categories