test.php file
<?php
if(isset($_POST['submit']))
{
if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
{
echo "Please select an image";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
saveimage($image);
}
}
function saveimage($image)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$query = "INSERT INTO info(image) VALUES ('$image')";
$result = mysql_query($query,$con);
if($result)
{
echo "<br> Image upload";
}
else
{
echo "<br> NOT";
}
}
function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("food",$con);
$query = "SELECT image from info";
$result = mysql_query($query,$con);
$row=mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.$row["image"].'" width="200" height="200"/>';
}
?>
html file
<form method="post" action="test.php">
<label>Image:</label>
<input type="file" name="image"><br>
<input class="btn btn-primary" type="submit" name="submit" value="Confirm" style="height:50px; width:100px;">
<br><br>
</form>
Now i want to display the picture. But i keep getting a blank picture instead. Is there any problem with my display code there? Thanks
/dummy//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//dummy
add enctype="multipart/form-data" in your form
<form method="post" action="test.php" enctype="multipart/form-data">
Your are missing enctype="multipart/form-data" in your form tag.
The enctype attribute specifies how the form data should be encoded when submitting it to the server.
Note: The enctype attribute can be used only with method="post".
Add enctype="multipart/form-data" to your form tag.
you make a POST request, you have to encode the data that forms the body of the request.
When writing client-side code,you need to use multipart/form-data if your form includes any elements.
Related
<form action="?action7" method="POST">
<label for="fname">
Update image
</label>
<input type="file" name="image">
<input type="submit" name="submit" value="Submit">
<?php
if(isset($_GET['action7'])=='rdt')
{
$image= addslashes($_FILES['image']['tmp_name']);
$image= file_get_contents($image);
$image= base64_encode($image);
saveimage($image);
}
function saveimage($image)
{
$con=mysqli_connect("localhost","root","","mydb");
mysqli_select_db($con,"photos");
$qry="update images102 set image='$image' where email='$picmail'";
$result=mysqli_query($con,$qry);
if($result)
{
echo "image uploaded.";
}
else
{
echo "image not uploaded";
}
}
?>
</form>
Please note: $picmail is a global variable which is declared outside this code function. The Execution shows the message "Image uploaded". However, the database shows no change in the older image
You should add enctype="multipart/form-data" in form element
<form action="?action7" method="post" enctype="multipart/form-data">
Still a newb to coding . have almost no idea what im doing . i tried to make a php page which would let me upload and view an image . do not know what is wrong . i tried to do it as correctly as possible . could someone please help me out ?
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected ";
$imagename= mysqli_real_escape_string($_FILES['image'] ['name']);
$imagefile =mysqli_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$qry = "INSERT INTO image (name,file) VALUES ('$imagename','$imagefile')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo "$currimage <br/>";
}
?>
</body>
</html>
To be able to catch a post variable, you need to submit the form and handle the action. The first problem with your code is that your form is not complete - it's missing a closing tag. Second thing, to be able to send a file through the post, you'll need multipart form. You should add enctype="multipart/form-data" as an attribute of the form.
So, instead of
<form method="post">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
You'll need
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
</form>
you must move the uploaded image to server
try this code -- create directory /uploads/
<!doctype html>
<html>
<head>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input type="submit" name="submit" value="upload"></input>
<?php
if(isset($_POST['submit']))
echo "button has been clicked";
$con = mysqli_connect("127.0.0.1","root","","demo");
if(!$con)
echo "didnt connect to database ";
else echo "connected";
$uploads_dir = '/uploads';
$tmp_name = $_FILES["image"]["tmp_name"];
$name = $_FILES["image"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$qry = "INSERT INTO image (name,file) VALUES ('$name','$tmp_name')";
$result = mysqli_query($con,$qry);
if($result)
echo "image has been uploaded";
viewimage();
function viewimage()
{$recon = mysqli_connect("127.0.0.1","root","","demo");
$view = "SELECT * FROM image ";
$data =mysqli_query($recon,$view);
$res2 =mysqli_fetch_assoc($data);
$currimage =$res2['file'];
echo '<img src="'.$currimage.'" /> <br/>';
}
?>
</body>
</html>
This code in "upload.php"
<?php
session_start();
$id = $_SESSION['user_id'];
$pic = $_FILES['pic']["name"];
$folder = "../images/users/";
move_uploaded_file($_FILES['pic']["tmp_name"], "$folder".$pic);
$mysqli = connectDB();
upload($id,$pic,$mysqli);
?>
Function upload
function upload($id,$pic,$mysqli)
{
$pic = $mysqli->real_escape_string($pic);
$sql = "UPDATE users SET pic = '$pic' WHERE id = '$id'";
$result = $mysqli->query($sql);
}
and this code in html
<form name="submit" action="include/upload.php" method="post">
<input type="file" value="Choose file" accept="image/*" id="pic" name="pic">
<input type="submit" value="Upload" name="submit">
</form>
It's not working. Image not move folder and image path not insert in database.
Instead of:
move_uploaded_file($_FILES['pic']["tmp_name"], "$folder".$pic);
You can just do:
move_uploaded_file($_FILES["pic"]["tmp_name"], $folder.$pic); // Note the single / double quotes
But even easier:
<?php
session_start();
$id = $_SESSION['user_id'];
$pic = $_FILES["pic"]["name"];
$folder = "../images/users/";
$path = $folder.$pic; // New variable
if( move_uploaded_file($_FILES["pic"]["tmp_name"], $path) ) {
$mysqli = connectDB();
if( upload($id, $path, $mysqli) ) {
echo 'File uploaded';
} else {
echo 'Something went wrong uploading file';
}
} else {
echo 'Something went wrong uploading file';
}
So the function becomes:
function upload($id, $path, $mysqli)
{
$path = $mysqli->real_escape_string($path);
$sql = "UPDATE `users` SET `pic` = '$pic' WHERE `id` = $id";
$result = $mysqli->query($sql);
return $result; // returns true or false
}
Adding backticks around table and column names prevents an error called mysql reserved words. Because the $id is an integer (in most cases). You shouldn't quote it. This is because you make a string from an integer which is something you don't want to do as integers are faster and saver to use.
To make html clear you want to upload a file you need to add something to your form. So the form becomes:
<form name="submit" action="include/upload.php" method="post" enctype="multipart/form-data">
<input type="file" value="Choose file" accept="image/*" id="pic" name="pic">
<input type="submit" value="Upload" name="submit">
</form>
form tag should be in the format :
< form name="submit" action="include/upload.php" method="post" enctype="multipart/form-data" >
(add enctype="multipart/form-data" in form tag, this will solve your problem.)
I am uploading an image using the following php code but the file is not getting upload.
if(isset($_POST['submit'])){
$title = $_POST['title'];
$target_folder = "../newsimageuploads/";
$bannerimagelink = "http://example.com/newsimageuploads";
$bannerimage = addslashes(file_get_contents($_FILES['bannerimage']['tmp_name']));
$bannerimage_name = addslashes($_FILES['bannerimage']['name']);
$bannerimage_size = getimagesize($_FILES['bannerimage']['tmp_name']);
if ($bannerimage!=""){
$rand = rand(111111, 9999999);
$fname = $_FILES["bannerimage"]["name"];
$newname = "Image ".$rand.".png";
move_uploaded_file($_FILES["bannerimage"]["tmp_name"], $target_folder.$newname);
$bannerimage_location = $bannerimagelink."/".$newname;
}
$query =mysql_query("INSERT INTO mytable (title,image) VALUES ('$title','$bannerimage_location')")or die(mysql_error());
if (($query) === TRUE) {
echo "<p style='color:green;'>Added Successfully</p>";
} else {
echo "Some Error Occured :(";
}
}
And my HTML part is
<form action="#" method="post">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
</form>
My title is getting insert in the MySQL table but image does not.
You are missing enctype='multipart/form-data' in your form
<form action="#" method="post" enctype="multipart/form-data">
Look here for more details
Add
enctype="multipart/form-data"
to the form tag. Without this attribute you will only get the name of the file. But the file itself won't be uploaded.
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
I have the following code which works after submitting my form:
I'm getting the photo which is upload, in my upload folder.
Now I want to Insert this photo name and path in my database.
Example:
If image name is 6.jpg, I want in my database name=6.jpg and path=uploads/6.jpg
html:
<form method="post" enctype="multipart/form-data">
<input type="file" name="foto">
<input type="submit" name="submit">
php:
<?php
if (isset($_POST['submit']) && empty($errors) === true )
{
$name=basename($_FILES['foto']['name']);
$t_name=$_FILES['foto']['tmp_name'];
$dir='upload';
if(move_uploaded_file($t_name,$dir."/".$name))
{
}
else
{
}
}
?>
<?php
if(move_uploaded_file($t_name,$dir."/".$name))
{
mysql_connect('localhost','username','password');
mysql_select_db('yourdb');
$path=$dir."/".$name;
$sql="INSERT INTO `your_table`(`name`,`path`) values ('".$name."','".$path."')";
$result=mysql_query($sql);
if($result){
echo"Successfully added to db";
}
}
else
{
echo "Sorry.File Upload failed";
}
?>