creating image host service using $php - 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>";
}
}
}

Related

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

Displaying an image from database mysql, PHP

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));

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

How can I save an image from a file input field using PHP & MySQL?

How can I save an image safely from a file input field using PHP & MySQL?
Here is the input file field.
<input type="file" name="pic" id="pic" size="25" />
This is a simple example, it should work.
Although you probably want to add checking for image types, file sizes, etc.
<?php
$image = $_POST['pic'];
//Stores the filename as it was on the client computer.
$imagename = $_FILES['pic']['name'];
//Stores the filetype e.g image/jpeg
$imagetype = $_FILES['pic']['type'];
//Stores any error codes from the upload.
$imageerror = $_FILES['pic']['error'];
//Stores the tempname as it is given by the host when uploaded.
$imagetemp = $_FILES['pic']['tmp_name'];
//The path you wish to upload the image to
$imagePath = "images/";
if(is_uploaded_file($imagetemp)) {
if(move_uploaded_file($imagetemp, $imagePath . $imagename)) {
echo "Sussecfully uploaded your image.";
}
else {
echo "Failed to move your image.";
}
}
else {
echo "Failed to upload your image.";
}
?>
http://php.net/file_upload covers just about everything you need to know.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tmpFile = $_FILES['pic']['tmp_name'];
$newFile = '/new_location/to/file/'.$_FILES['pic']['name'];
$result = move_uploaded_file($tmpFile, $newFile);
echo $_FILES['pic']['name'];
if ($result) {
echo ' was uploaded<br />';
} else {
echo ' failed to upload<br />';
}
}
?>
<form action="" enctype="multipart/form-data" method="POST>
<input type="file" name="pic" />
<input type="submit" value="Upload" />
</form>

Categories