php image not uploading to database - php

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.

Related

Multiple values from dropdown to single column insert with SPACE php mysql

OK, I am trying to insert multiple values into a single column catName with space. But I am unable to insert it..here is my code. suppose I m selected sports cricket football from the dropdown and all three name should be there in column with space only not comma.
<?php
//include config
require_once('includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
require_once('includes/config2.php');
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$picCourtesy = $_POST["picCourtesy"];
$picTitle = $_POST["picTitle"];
$catName = implode(' ',$_POST['$catName']);
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into gallery (`picTitle`,`picCourtesy`,`catName`,`gFILE_NAME`,`gFILE_SIZE`,`gFILE_TYPE`) VALUES('$picTitle','$picCourtesy','$catName','$file_name','$file_size','$file_type'); ";
$desired_dir="../gallery";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysqli_query($conn,$query);
}else{
print_r($errors);
}
}
if(empty($error)){
header('Location: index.php');
exit;
}
}?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin - Add Post</title>
<link rel="stylesheet" href="style/normalize.css">
<link rel="stylesheet" href="style/main.css">
</head>
<body>
<div id="wrapper">
<?php include('menu.php');?>
<form action="" method="POST" enctype="multipart/form-data">
<p>upload single pic, Less than 2mb.</p>
<p><label>Title</label><br />
<input type='text' name='picTitle' value='<?php if(isset($error)){ echo $_POST['picTitle'];}?>'></p>
<p><label>photo courtesy</label><br />
<input type='text' name='picCourtesy' value='<?php if(isset($error)){ echo $_POST['picCourtesy'];}?>'></p>
<?php
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT * FROM category";
$result = mysqli_query ($conn,$query);?>
<p><label >Category:</label><br/>
<select multiple="multiple" name="catName[]">
<?php
while($r = mysqli_fetch_array($result)) {
echo '<option value='.$r['catName'].'>'.$r['catName'].'</option>';
}
echo "</select>";
?></p>
<input type="file" name="files[]" />
<input type="submit"/>
</form>
</div>
</body>
Please help me, hope you understand my problem here
just replace
$catName = implode(' ',$_POST['$catName']);
with
$catName = implode(' ',$_POST['catName']);
By mistakely You just add '$' on $_POST['$catName']

creating image host service using $php

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>";
}
}
}

Image not being Uploaded in MySQL

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.

After I upload my image it doesnt get added into the folder

I am creating a function so that you can upload an image and I want the image you selected to be stored in a folder on the same domain.
When I try the code below and upload an image I succesfully get "Image Succesfully Uploaded!" but the image does not get added inside my folder: "MyMap/MyPhotoMap/$image_name".
<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'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else
move_uploaded_file($image_tmp_name, "MyMap/MyPhotoMap/$image_name");
echo "Image Succesfully Uploaded!";
}
?>
try this:
<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'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
if(move_uploaded_file($image_tmp_name, "MyMap/MyPhotoMap/$image_name")); {
echo "Image Succesfully Uploaded!";
}
else{
echo "Image not uploaded" ;
}
}
?>
You need proper braces;
if($image_name==''){
echo "You forgot to select an image. Please choose one!";
exit();
}
else { // add braces for else part
move_uploaded_file($image_tmp_name, "MyMap/MyPhotoMap/$image_name");
echo "Image Succesfully Uploaded!";
}

Notice: Undefined index: image - unable to find the error

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

Categories