image inside database saved as BLOB name - php

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:

Related

How can I save an image in a directory with php

noob programer here. I'm doing a form and my teacher asked me to upload an image from any location of my computer and save it in a specific directory(also of my commputer). The code that i have only works if the image is in the same location as my script. Hope you can help me.
Note: Also i have to save it in the database because i'm going to use it to make a pdf.
HTML:
<html>
<head>
</head>
<form method="post" action="IConductores.php" >
<p></p>
<label>Foto</label>
<input type="file" id="Foto" name="Foto">
<input type="submit" id="Enviar" name="Enviar">
</form>
</html>
IConductores.php:
<?php
$Rfc = $_POST['Rfc'];
$Foto = $_POST['Foto'];
$SQL = "INSERT INTO Conductores VALUES ('$Rfc', '$Foto');";
$destdir = 'ImagenesPerfil/'; // path destination
$img=file_get_contents($Foto);
file_put_contents($destdir.substr($Foto, strrpos($Foto,'/')), $img);
add enctype="multipart/form-data" in your form tag
<form method="post" action="IConductores.php" enctype="multipart/form-data">
Uploaded file's name will be in $_FILES["Foto"]["name"]
And uploaded file location will be $_FILES["Foto"]["tmp_name"]
You need to use move_uploaded_file function like this
move_uploaded_file($_FILES["Foto"]["tmp_name"], $destdir.$_FILES["Foto"]["name"])

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.

Image not insert in database using php files

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']

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"];
?>

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