Image not insert in database using php files - php

The images column in my database is blank when I click the upload button also .... I tried using $_POST['images'] then the image is stored in db but thats of no use the error is:
<code>
<html>
<head>
<title> Image testing...</title>
</head>
<body>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="file" name="images" id="images" accept="image/png, image/jpeg" />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
<?php
$conn = mysqli_connect('localhost', 'root', '', 'test_event');
if(isset($_POST['submit']))
{
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO products (id,image) VALUES('','$image')";
$qry = mysqli_query($conn,$query);
$sql = "SELECT * FROM products";
$sth = $conn->query($sql);
$result = mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg:base64,'.base64_encode($result['image']).'"/>';
}
else{
echo "error";
}
?>
</code>
Notice: Undefined index: images in C:\xampp\htdocs\admin\image.php on
line 17
Warning: file_get_contents(): Filename cannot be empty in
C:\xampenter code herep\htdocs\admin\image.php on line 17**

Add
enctype="multipart/form-data"
to your form tag.
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
More info
https://www.w3schools.com/tags/att_form_enctype.asp

You need to add the enctype tag to the form to tell PHP and the browser how to interpret the form data.
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="images" id="images" accept="image/png, image/jpeg" />
<input type="submit" name="submit" value="Upload" />
</form>
The files metadata will then be available at $_FILES['images']

Related

Can't display image I uploaded in PHP

I'm trying to upload an image and display after uploading, the upload part works fine but image can't display.
Any answers?
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo <<<_END
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
_END;
if($_FILES){
$name = $_FILES['fupload']['name'];
move_uploaded_file($name = $_FILES['fupload']['tmp_name'], $name);
echo "<br><img src='$name'>";
}
?>
</body>
</html>
Browser:
Image can't display
nevermind, the problem is move_upload ($name=xxxx, $name), it means you assign to $name the tmp source !
here is a working code
<!DOCTYPE html>
<html>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
<?php if($_FILES)
{
$source=$_FILES['fupload']['tmp_name'];
$target1 = $_FILES['fupload']['name'];
move_uploaded_file($source,$target1);
?>
<br>
source=<?php echo htmlspecialchars($source);?>
<br>
target=<?php echo htmlspecialchars($target1);?>
<img src="<?php echo htmlspecialchars($target1);?>"
<?php
} // if $_FILES
?>
</body>
</html>
Ok, following comment, it seems $name point To à path not accessible for external user. Try a link like this $name="c:\path To your base path\www\et.png"
Edit: supposing you have a existing www folder , where you find your index.php. It may be called public.

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>

image inside database saved as BLOB name

I'm new to PHP. I've uploaded the image inside the database but the image is saved in the name BLOB. So it is difficult for me to retrieve the image back. Please help me out on how to save the image in its actual name.
Following is my code.
<?php
require_once 'config.php'
?>
<html>
<head>
<title> image</title>
<body>
<form action="hello.php" method="post" enctype="multipart/form-data">
<label> Profile Image </label><br><br>
<input type="file" name="image" id="image" required autocomplete="off"/>
<input type="submit" value="upload" name="upload" >
</form>
<?php
if(isset($_POST['upload']))
{
$filetmp=$_FILES["image"]["tmp_name"];
$filename=$_FILES["image"]["name"];
$filetype=$_FILES["image"]["type"];
$filepath="image/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql="INSERT INTO image(img,path,type) VALUES('$filename','$filepath','$filetype')" ;
mysqli_query($db, $sql);
echo"image uploaded";
}
?>
</body>
</html>
This is the output:

PHP uploaded file name doesnt show up in browser

All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>

Save image as session and display it

I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable $ _FILES
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>

Categories