database - Issue Upload Image in Folder using PHP -
i'm struggling how upload picture in folder called uploads , insert name of image in database. know how make work? thanks
index.php
<form action="index.php" method="post" enctype="multipart/form-data"> select image upload: <input type="file" name="image" id="image"> <input type="submit" value="upload image" name="submit"> </form>
upload.php
// connect localhost project 1 $host = 'localhost'; $db = 'pju2173'; $user = 'pju2173'; $pwd = 'fr1end'; try{ $dbh = new pdo("mysql:host=$host;dbname=$db", $user, $pwd); $dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception); } catch(pdoexception $e){ return 'connection failed: ' . $e->getmessage(); } if(isset($_post['submit'])){ $image = $_files['image']['name']; $target_dir = "uploads/"; $target_file = $target_dir . basename($_files['image']['name']); $imagefiletype = pathinfo($target_file, pathinfo_extension); // move_uploaded_file($_files['image']['tmp_name'], $target_file); move_uploaded_file($_files['image']['tmp_name'], $target_file.time().$image); $query = "insert imagetable(image) values(:image)"; $stmt = $dbh->prepare($query); $stmt->bindparam(":image", $image); $result = $stmt->execute(); if($result){ echo "uploaded"; } else{ echo "something wrong"; } }
this possible helps
http://www.w3schools.com/php/php_file_upload.asp
complete upload file php script
<?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image if(isset($_post["submit"])) { $check = getimagesize($_files["filetoupload"]["tmp_name"]); if($check !== false) { echo "file image - " . $check["mime"] . "."; $uploadok = 1; } else { echo "file not image."; $uploadok = 0; } } // check if file exists if (file_exists($target_file)) { echo "sorry, file exists."; $uploadok = 0; } // check file size if ($_files["filetoupload"]["size"] > 500000) { echo "sorry, file large."; $uploadok = 0; } // allow file formats if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif" ) { echo "sorry, jpg, jpeg, png & gif files allowed."; $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) { echo "sorry, file not uploaded."; // if ok, try upload file } else { if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) { echo "the file ". basename( $_files["filetoupload"]["name"]). " has been uploaded."; } else { echo "sorry, there error uploading file."; } } ?>
Comments
Post a Comment