ROCKINDANO Posted March 10, 2009 Share Posted March 10, 2009 Hello all, well i am working on a project where you upload stories to a db and display them on a page. now i have my upload form page with my text boxes and all. and i can upload the stories and display them but now i want to add images through this upload form page. can anybody point me to the right direction as of how to go about this. Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/148846-inserting-pictures/ Share on other sites More sharing options...
premiso Posted March 10, 2009 Share Posted March 10, 2009 Generally you should put the picture in a directory then put the file information in a DB. It is not "good" practice to put images in the DB. Although if that sounds preferable you would use the BLOB data type. For more on how to read them back to the page readfile and header for that part. Quote Link to comment https://forums.phpfreaks.com/topic/148846-inserting-pictures/#findComment-781626 Share on other sites More sharing options...
socratesone Posted March 10, 2009 Share Posted March 10, 2009 A couple things: First, you want to alter the form to include the enctype: <form id="whatever" action="whatever" enctype="multipart/form-data" > Then, you want your file field in the form itself: <input type="file" name="image_file" /> Next, you may want to do some javascript validation to make sure it's the right file type. If you have a one-to-one relationship between post and image (one image per post), you will add the field to the database. Finally, you edit your form processing script to include the file upload: // set the path $path = 'path/to/save/filename.jpg'; // handle the upload move_uploaded_file($_FILES['image_file']['tmp_name'], $path) ; // enter into database mysql_query("UPDATE table set `image_path`= '$path'); Note that this is VERY simple, and is NOT meant to be a cut and paste solution. You will probably want to do more to validate server-side, check for existing files to avoid over-writing them, and more. A more in-depth discussion can be found here: http://www.webdeveloper.com/forum/showthread.php?t=101466 Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/148846-inserting-pictures/#findComment-781640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.