I have a form and the person who fill it has the option to upload an image. The information is then stored in a table, with the person's name, e-mail, etc. and later shown in a page for the admin. I'm able to pull all the information, except I don't know how to store the image into the database and show it for the admin later. Hope you guys could understand
You don't store the images in the database, you store there path as a string
There is global variable called $_FILES
just as $_GET
and $_POST
that contains all the info on the files uploaded. These are first stored in a temp directory. You use move_uploaded_files
function to move them to a proper place. Then save the string in database.
<?php
$uploads_dir = '/uploads'; // Directory where you want to upload/keep all your pictures
// Lets assume you sent the picture in `pictures` name field of form
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
// Save `$upload_dir/$name` in db
}
}
?>
Now when you want to load the image later, just query the column of the table that stores the images location and use it as src
at frontend.