This all started with me getting an unidentified index error: image. Then I put in the check before so I would not have to see the error again. However I know the images are not being uploaded. I have used legit images to test, and nothing is working. I have been struggling on this for hours. Please Help.
<html>
<body>
<form action="images-add.php" method="post" enctyp="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="sum" value="upload" />
</form>
</body>
</html>
<?php
include('connection.php');
if(isset($_POST['image'])) {
if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name']) == FALSE){
$image= addslashes($_FILES(['image']['tmp_name']));
$name= addslashes($_FILES(['image']['name']));
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
}
function saveimage($name, $image){
$con= mysql_connect("localhost", "root", "");
mysql_select_db("hw2", $con);
$qry= "insert into images (name, image) values ('$name', '$image')";
$result= mysql_query($qry,$con);
if($result) {
echo "<br/> Image Uploaded.";
}else{
echo "Not Uploaded";
}
}
?>
Please change enctyp to enctype in your HTML form.
Also update code
From
$image= addslashes($_FILES(['image']['tmp_name']));
To
$image= addslashes($_FILES['image']['tmp_name']);
And
This line of code, From
$name= addslashes($_FILES(['image']['name']));
To
$name= addslashes($_FILES['image']['name']);
Complete code is given below...
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="sum" value="upload" />
</form>
</body>
</html>
<?php
include('connection.php');
if($_POST){
if(empty($_FILES) || !isset($_FILES['image'])){
echo "Please Add an Image";
}else{
if(getimagesize($_FILES['image']['tmp_name'])){
$image= addslashes($_FILES['image']['tmp_name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
}
function saveimage($name, $image){
$con= mysql_connect("localhost", "root", "");
mysql_select_db("hw2", $con);
$qry= "insert into images (name, image) values ('$name', '$image')";
$result= mysql_query($qry,$con);
if($result) {
echo "<br/> Image Uploaded.";
}else{
echo "Not Uploaded";
}
}
?>
I hope this code will works for you.
Related
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>";
}
}
}
The error that i get when i try to upload it is this error:
Warning: file_get_contents(MTUtODAwLmpwZw==): failed to open stream:
No such file or directory in
C:\xampp\htdocs\miniproject\imageupload\image.php on line 24
Also i already have a picture inserted in my database and when it is retrieve it just show a plank picture.
This is my code
<?PHP
ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<input type="file" name="image"/>
<br/><br/>
<input type="submit" name="sumit" value="Upload"/>
</form>
<?php
if(isset($_POST['sumit']))
{
if (getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image.";
}
else
{
$image= addslashes($_FILES['image']['name']);
$name= addslashes($_FILES['image']['name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($name, $image);
}
}
displayimage();
function saveimage($name,$image)
{
$con= mysql_connect("localhost:3307", "root", "");
mysql_select_db("kstark",$con);
$qry="insert into images (name, image) values ('$name','$image')";
$result= mysql_query($qry,$con);
if ($result)
{
echo "<br/>Image uploaded.";
}
else
{
echo "<br/>Image not uploaded.";
}
}
function displayimage()
{
$con= mysql_connect("localhost:3307", "root", "");
mysql_select_db("kstark",$con);
$qry="select * from images";
$result= mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row[2].'">';
}
mysql_close($con);
}
?>
</body>
You need to use the tmp_name rather than the filename when you attempt to retrieve the contents.
if(isset($_POST['sumit']))
{
if (getimagesize($_FILES['image']['tmp_name'])==FALSE) {
echo "Please select an image.";
} else {
/* use the tmp_name rather than name here! */
$image=file_get_contents( $_FILES['image']['tmp_name'] );
$image=base64_encode($image);
$name=addslashes($_FILES['image']['name']);
saveimage($name,$image);
}
}
I'm trying to upload an image to a database using a form.
The problems is that when i try to upload the image, it isn't stored in the database. Also there is no error.
Thanks in advance.
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" value="Upload" />
</form>
<?php
require "connect.inc.php";
#$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['tmp_name']);
$imageName = $_FILES['image']['tmp_name'];
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = "INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')";
if (!mysql_query("INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')")) {
echo "Problem uploading image.";
}
else {
echo "Succes!";
}
}
}
?>
</body>
</html>
You have forgot to execute your query :
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" value="Upload" />
</form>
<?php
require "connect.inc.php";
#$file = $_FILES['image']['name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['name']);
$imageName = $_FILES['image']['tmp_name'];
$img = $_FILES['image']['name'];
$imageSize = getimagesize($_FILES['image']['name']);
//add move uploaded file function here
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = mysql_query("INSERT INTO `afbeelding` VALUES ('', '$img', '$image')"); // did changes here
if($sql){
echo "Succes!";
}
else {
echo "Problem uploading image.";
}
}
}
?>
</body>
</html>
I have not tested, but will work
<?php
require "connect.inc.php";
if(isset($_POST['submit']))
{
#$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {
echo "Please select an image.";
}
else {
$image = file_get_contents($_FILES['image']['tmp_name']);
$imageName = $_FILES['image']['tmp_name'];
$imageSize = getimagesize($_FILES['image']['tmp_name']);
if ($imageSize == FALSE) {
echo "This is not an image.";
}
else {
$sql = "INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')";
if (!mysql_query("INSERT INTO `afbeelding` VALUES ('', '$imageName', '$image')")) {
echo "Problem uploading image.";
}
else {
echo "Succes!";
}
}
}
}
?>
<html>
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
The problem was not having the addslashes(). In: $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
If i remove them again it won't work but if i put them back in it succeeds.
Following is my code file...
I am not able to insert the image. Getting an error as, Undefined variable: image
<html>
<head>
<title> Upload an image </title>
</head>
<body>
<form action="image-disp.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="image" value=iamge><br>
<input type="submit" value="Upload">
</form>
<?php
mysql_connect("localhost", "root", " ") or die(mysql_error());
mysql_select_db("mysql") or die(mysql_error());
echo "connected";
if (!isset($_FILES['image'] ['tmp_name'])) {
echo "Choose an image";
} else {
echo $image = addslashes($_FILES['image'] ['tmp_name']);
echo $image_name = addslashes($_FILES['image']['name']);
echo $image_size = getimagesize($_FILES['image'] ['tmp_name']);
}
if ($image_size = FALSE) {
echo "It's not an image";
} else {
$result = "INSERT INTO testblob (image_id, image, image_size) "
. "VALUES (' ' ,'$image', '$image_size')";
}
echo "inserted";
?>
</body>
</html>
better save just the name of the image in database, and the file in some folder
$result = "INSERT INTO testblob ( image, image_size)
VALUES ('$image_name', '$image_size')";
then you retrieve it like that:
<img src="path/<?php echo $row['image_name'];?>">
First create a table.
Mysql-Query
CREATE TABLE storeimage ( image_id tinyint(3) NOT NULL AUTO_INCREMENT, image blob NOT NULL, KEY image_id (image_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Code to store image in database:
<html>
<body>
<form action="image-disp.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" method="POST">
<input type="submit" value="submit" name= "submit">
</form>
<?php
mysql_connect("localhost", "root", "root") or die (mysql_error());
mysql_select_db("mysql") or die (mysql_error());
if (isset($_FILES['image']['tmp_name']))
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
}
if(isset($_POST['submit']))
{
$insert = mysql_query("INSERT INTO storeimage VALUES ('', '$image')");
echo " file inserted";
}
?>
</body>
</html>
Your error is coming from PHP, not MySQL. It's saying your variable, $image, isn't defined because it's not filled with anything.
Your code isn't going to store the actual image, but instead, the filename.
You need to remove the space.
echo $image = addslashes($_FILES['image']['tmp_name']);
Also, you don't even have it executing a query...
if ($image_size == FALSE) {
echo "It's not an image";
} else {
$result = mysql_query("INSERT INTO testblob (image_id, image, image_size) "
. "VALUES (' ' ,'$image', '$image_size')");
}
Take a look here for a basic example: http://www.w3schools.com/php/php_file_upload.asp
here is my error.i dont know why it is not working.i checked all the parts but i was unable to find the error.
Notice: Undefined index: image in C:\xampp\htdocs\Final\places\Ressave.php on line 27
here is my html code that pass the name of input:
<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on">
<div class="well">
<legend>Photos</legend>
<div class="control-group">
<label class="control-label">Upload Photo: </label>
<div class="controls">
<input name="image" type="file" />
</div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn">Cancel</button>
</div>
</form>
Ressave.php is here and can not recieve the name here,so the error occure.....
<?php
{ // Secure Connection Script
include('../Secure/dbConfig.php');
$dbSuccess = false;
$dbConnected = mysql_connect($db['hostname'],$db['username'],$db['password']);
if ($dbConnected) {
$dbSelected = mysql_select_db($db['database'],$dbConnected);
if ($dbSelected) {
$dbSuccess = true;
} else {
echo "DB Selection FAILed";
}
} else {
echo "MySQL Connection FAILed";
}
// END Secure Connection Script
}
if(! $dbConnected )
{
die('Could not connect: ' . mysql_error());
}
{ // File Properties
$file = $_FILES['image']['tmp_name']; //Error comes from here(here is the prob!)
if(!isset($file))
echo "Please Choose an Image.";
else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size == FALSE)
echo "That is not an image.";
else
{
$lastid = mysql_insert_id();
echo "Image Uploaded.";
}
}
}
{ //join the post values into comma separated
$features = mysql_real_escape_string(implode(',', $_POST['features']));
$parking = mysql_real_escape_string(implode(',', $_POST['parking']));
$noise = mysql_real_escape_string(implode(',', $_POST['noise']));
$good_for = mysql_real_escape_string(implode(',', $_POST['good_for']));
$ambience = mysql_real_escape_string(implode(',', $_POST['ambience']));
$alcohol = mysql_real_escape_string(implode(',', $_POST['alcohol']));
}
$sql = "INSERT INTO prestaurant ( ResName, Rating, Food_serve, Features, Parking, noise, Good_For, Ambience, Alcohol, Addition_info, Name, Address1, Zipcode1, Address2, Zipcode2, Address3, Zipcode3, City, Mobile, phone1, phone2, phone3, phone4, Email1, Email2, Fax, Website, image)".
"VALUES ('$_POST[restaurant_name]','$_POST[star_rating]','$_POST[food_served]','$features','$parking','$noise','$good_for','$ambience','$alcohol','$_POST[description]','$_POST[name]','$_POST[address1]','$_POST[zipcode1]','$_POST[address2]','$_POST[zipcode2]','$_POST[address3]','$_POST[zipcode3]','$_POST[city]','$_POST[mobile]','$_POST[phone1]','$_POST[phone2]','$_POST[phone3]','$_POST[phone4]','$_POST[email1]','$_POST[email2]','$_POST[fax]','$_POST[url]','$image')";
mysql_select_db('place');
$retval = mysql_query( $sql );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($dbConnected);
?>
If a file was not uploaded the $_FILES array will be empty. Specifically, if the file image was not uploaded, $_FILES['image'] will not be set.
So
$file = $_FILES['image']['tmp_name']; //Error comes from here(here is the prob!)
should be:
if(empty($_FILES) || !isset($_FILES['image']))
update
You will also have issues because you're missing the enctype attribute on your form:
<form class="form-horizontal" action="Ressave.php" method="POST" autocomplete="on" enctype="multipart/form-data">
In order to be able to process files in your form you need to add the enctype attribute.
<form method='POST' enctype='multipart/form-data' >
Hey i guess you have forgotten one important setting in form
enctype="multipart/form-data" this option is used when used with files eg image file etc
<form name="image" method="post" enctype="multipart/form-data">
Apart from that to extract the contents of the file you can use following options
$tmp_img_path = $_FILES['image']['tmp_name'];
$img_name = $_FILES['image']['name'];
to print all the content of a file use:
print_r($_POST['image']);
this is the code i have which successfully insert the data into mysql and retrieve from the DB.
"Index.PHP"
<html>
<head>
<title>PHP & MySQL: Upload an image</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File: <input type="file" name="image" /><input type="submit" value="Upload" />
</form>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("registrations") or die(mysql_error());
if(!isset($_FILES['image']))
{
echo 'Please select an image.';
}
else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
echo $_FILES['image']['tmp_name'];
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE){
echo "That's not an image.";
} else {
if(!$insert = mysql_query("INSERT INTO test_image VALUES ('','$image_name','$image')"))
{
echo "Problem uploading image.";
} else {
$lastid = mysql_insert_id();
echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
this is get.PHP
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("registrations") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM test_image WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
NOTE:- please changes your DB and table name accordingly..
Thanks,
Santanu