$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.
Related
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();
I'd like to include in my MySQL table a path to an image. The image path gets into the table by inserting the value of a "file" textfield (one of those Browse kind of deals). So the value that gets entered is something like: image001.jpg. But I can't seem to use that value to put an image into a html page. if it goes into the table fine, why can't I get it out?
I upload an image but I don't know where it's gone. Because there's no value entered in image field when I checked it through PhpMyadmin.
Table schema
CREATE TABLE employee_details
(
emp_image varchar(255),
employee_name varchar(50),
employee_address varchar(50),
employee_designation varchar(50),
employee_salary int(),
);
Query
$sql="
INSERT INTO employee_detail(
emp_image,
employee_name,
employee_address,
employee_contact,
employee_designation,
employee_salary
)
VALUES(
'$_POST[emp_image]',
'$_POST[employee_name]',
'$_POST[employee_address]',
'$_POST[employee_contact]',
'$_POST[employee_designation]',
'$_POST[employee_salary]'
)";
On your comment you ask how to upload and store the data to mysql. So here it is:
To get the file, you should have a script in your html like this:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Now, on POST, your PHP file should look like this but please take note that you have to check if the file exists on your POST:
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
Since the "Stored in:" part is just the temporary path, you should move to your 'real' image path using move_uploaded_file().
Let say the real/default path for your images is in:
$image_dir= '/images/';
You just have to move the file using this:
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $image_dir. $_FILES['uploaded_file']['name']);
And your full path to the image would be
$image = $final_save_dir . $_FILES['uploaded_file']['name'];
There are several ways to store the path to your database:
1st: Is to store just the filename and concatenate the path of the image in PHP using $_SERVER['DOCUMENT_ROOT'] and your default image path like:
$sql="insert into employee_detail( emp_image, employee_name, employee_address,
employee_contact, employee_designation, employee_salary)
values( '$image', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]',
'$_POST[employee_designation]','$_POST[employee_salary]')";
2nd: Is to store the full path like:
$sql="insert into employee_detail( emp_image, employee_name, employee_address,
employee_contact, employee_designation, employee_salary)
values( '".$_SERVER['DOCUMENT_ROOT']."\\images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]',
'$_POST[employee_designation]','$_POST[employee_salary]')";
What I recommend is this approach wherein you will input the partial path (without the root dir) so that later you don't have a problem on deploying it:
$sql="insert into employee_detail( emp_image, employee_name, employee_address,
employee_contact, employee_designation, employee_salary)
values( 'images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]',
'$_POST[employee_designation]','$_POST[employee_salary]')";
And make sure that the images are successfully upload to that default image dir/path.
UPDATE
I also recommend that you use mysqli_* or PDO and use prepare() method /function to prevent sql injection.
If you upload an image, it is saved in the temporary path at first. You'll have to move it to your final directory, otherwise it'll be gone after script execution. What basically happens is this:
If you have a form with some form fields (text, checkbox, textarea, whatever), AND a file field (<input type="file" name="uploaded_file" />), all 'normal' fields will be accessible with the $_POST array. The file(s) however will be accessible in the $_FILES array (see also the man page about file uploads).
Now when you receive the POST request, the uploaded files are stored in your temporary directory. If you don't do anything, it'll be deleted again after script execution. So you'd need to call the move_uploaded_file() function. Example:
$final_save_dir = '/path/to/images/dir/';
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $final_save_dir . $_FILES['uploaded_file']['name']);
And your full path to the image would be
$image_full_path = $final_save_dir . $_FILES['uploaded_file']['name'];
This path can be saved in your database:
$sql="
INSERT INTO employee_detail(
emp_image,
...
)
VALUES(
'$image_full_path',
...
)";
IMPORTANT: please take note on #brewal's comment. Your script is VERY unsafe like this.
I created a login page that when user login redirect to a pictures.php,
pictures page should contains the images of the user , I already create upload page to upload image to file directory and when the user upload an image the image link is added to a new row in the database, I need to add the image link to an exist row of the user,I already created a session for user when logged in.
So I need to add the image link to an exist row of the user.
This is the code that insert the image to a file directory and save its link to a new row of the database.
<?php
$name = $_FILES["myfile"] ["name"];
$type = $_FILES["myfile"] ["type"];
$size = $_FILES["myfile"] ["size"];
$temp = $_FILES["myfile"] ["tmp_name"];
$name = $size.$size .$name ;
$error = $_FILES["myfile"] ["error"];
if ($error > 0){
die ("Error uploading image");
}else{
mysql_query("INSERT INTO userid (imageid) VALUES ('.$name')");
move_uploaded_file($temp,"uploaded/".$name);
echo "Upload Completed";
}
?>
And the upload form is
<html>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="Submit" value="Upload">
</form>
</html>
So how to the image link to an exist row of the user.
Use mysql_query("UPDATE $table_name SET imageid='{$name}' WHERE userid=$userid")
Of course substituting variables and columns with correct values
Use UPDATE query instead of INSERT. INSERT will add new row but UPDATE will update your existing data.
You have to update the row if the row where you want to add image link is already exist. So use UPDATE Query. and debug wether table is updated or not.
check in table or use or die(); after the query
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 ;)
I have a form with a file input field:
<tr align="left">
<td>Image :</td>
<td align="left">
<input type="file" name="ImageFile" size="18">
</td>
</tr>
I then do stuff with this image file on submit:
$image_tmpname = $_FILES['ImageFile']['name'];
$imgdir = "blogImages/";
$imgname = $imgdir.$image_tmpname;
$blogs = new Blogs();
move_uploaded_file($_FILES['ImageFile']['tmp_name'], $imgname);
$insert = $blogs->insertBlog($heading, $article, $date, $imgname);
The directory I want to save the images is called blogImages and is in the same directory as the above.
You may notice in the above I call on a function called insertBlog within the class Blogs'. Insert blog takes all the info and inputs the data to a mysql table. The code forinsertBlog` is:
function insertBlog($heading, $article, $date, $imgname){
$query = "INSERT INTO Blogs (BlogTitle, MainArticle, PostDate, Image) VALUES ('$heading', '$article', '$date', '$imgname')";
$oDatabase = new database;
$connection = $oDatabase->Connect();
if (!mysql_select_db($oDatabase->Name(), $connection))
$oDatabase->ShowError("Blogs.insertBlog");
if (!(# mysql_query ($query, $connection)))
$oDatabase->ShowError("Blogs.insertBlog");
return (mysql_insert_id());
}
When a user fills out a form, it stores all the other information correctly in the MySQL table apart from the image information. Also, it doesn't store the actual image in the blogImages folder. So how do I get this script to upload the image to the blogImages folder and store its path in the mysql table. At the moment it doesnt not store the image in the db and only put the value blogImages/ in the image path field in my MySQL table.
To transfer a file you must specify multipart/form-data enctype in HTML.
<form enctype="multipart/form-data" action="" method="POST">