I made a page which I can upload an image file but I want to be able to add some details with picture and upload it at the same time and I dont have any idea how can I upload details from a text box with picture in to my table.
Here is my index.php
code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa-IR">
<head>
<meta name="author" content="www.hamyarprojects.com" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload file</title>
<link href="css/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="mainframe" >
<div id="lp" class = "panel panel-primary">
<div class = "panel-heading">
<h3 class = "panel-title">Upload file test</h3>
</div>
<div class = "panel-body">
<form action="Upload.php" method="post" enctype="multipart/form-data">
<p id="btn_box">
<input type="file" name="upload_image" />
</p>
<p id="btn_box">
<input type="submit" value="Upload Photo" name="submit">
<input type="reset" value="Clear" name="reset">
</p>
</form>
</div>
</div>
</div>
</body>
</html>
and here is upload.php
code
<link href="css/Style.css" rel="stylesheet" type="text/css">
<div class="main">
<?php
if (!isset($_FILES['upload_image'])) {
echo '<p>Please select a image</p>';
} else {
try {
upload();
echo '<p>Upload successfully</p>';
} catch(Exception $e) {
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload() {
if (is_uploaded_file($_FILES['upload_image']['tmp_name'])
&& getimagesize($_FILES['upload_image']['tmp_name']) != false
) {
$size = getimagesize($_FILES['upload_image']['tmp_name']);
$type = $size['mime'];
$imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['upload_image']['name'];
$maxsize = 99999999;
if ($_FILES['upload_image']['size'] < $maxsize ) {
$dbh = new PDO("mysql:host=localhost;dbname=bmc", 'root', '');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("INSERT INTO imageblob (image_type, "
. "image, image_size, image_name) VALUES (? ,?, ?, ?)");
$stmt->bindParam(1, $type);
$stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
$stmt->bindParam(3, $size);
$stmt->bindParam(4, $name);
$stmt->execute();
} else {
throw new Exception("size invalid");
}
} else {
throw new Exception("invalid format !");
}
}
</div>