sayr Posted January 8, 2007 Share Posted January 8, 2007 Hello,I have been looking for a good and as simple code as possible for image upload form and yet I haven't found a good one..I was wondering that some of you might have some good and simple codes for this one.So what I wanted is a php & html code which has form for image uploading, after the image is selected from your pc, there is "send" button, after that it automaticly adds the picture into mysql database.Im also curious to hear your toughts about the field type in database, someone has recommended to use BIN type, but I have also heared people talking about a picture link (didn't really get that one) If you have ideas for this on too, do tell!Thanks! Link to comment https://forums.phpfreaks.com/topic/33320-image-to-mysql-database/ Share on other sites More sharing options...
ted_chou12 Posted January 8, 2007 Share Posted January 8, 2007 This is what I am using:[code] <?php if(isset($_POST['submit'])){ //checks file extension $filename = $_FILES['uploadedfile']['name']; $ext = explode(".", $filename); if ($ext[1] != "jpg" && $ext[1] != "gif" && $ext[1] != "png" && $ext[1] !="jpeg") {echo "<p><b>The file you uploaded does not have an image extension!</b></p>";} else { // Where the file is going to be placed $target_path = "images/photo/"; $datetime = date("Y-m-d H:i:s"); $counter = ("images/photo/photonumber.txt"); $hits = file($counter); $hits[0] ++; $fp = fopen($counter , "w"); fputs($fp , "$hits[0]"); fclose($fp); $number = file_get_contents("images/photo/photonumber.txt"); /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = $target_path . basename($_FILES['uploadedfile']['name']); $photoname = $ext[0]; $target_path = "images/photo/"; $target_path = $target_path . basename("photo".$number.".jpg"); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {require("../mysqlconnection.php"); $insert = mysql_query("INSERT INTO photo(username, photonumber, photoname, photodate) VALUES('$username', '$number', '$photoname', '$datetime') ");} else {echo"<p><b>There was an error uploading the image, please try again!</b></p>";} if($insert === false) {echo"<p><b>There was an error uploading the image, please try again!</b></p>";} else {require("../mysqlconnection.php"); $read = mysql_query("SELECT id FROM photo WHERE photonumber='$number' AND username='$username'"); $r = mysql_fetch_array($read); $id = $r['id']; header("location: confirm.php?confirm=photoedit_upload&id=$id");}}}?> <form enctype="multipart/form-data" action="" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" name=submit value="Upload File" /> </form>[/code] Link to comment https://forums.phpfreaks.com/topic/33320-image-to-mysql-database/#findComment-155691 Share on other sites More sharing options...
matto Posted January 8, 2007 Share Posted January 8, 2007 MySQL field type of mediumblob should do the trick (can hold up to 16MB) Link to comment https://forums.phpfreaks.com/topic/33320-image-to-mysql-database/#findComment-155707 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.