robinrl2008 Posted August 19, 2008 Share Posted August 19, 2008 Hello, I have been trying to accomplish uploading an image and save it to mysql, so far I can't seem to get anywhere. I have looked at several tutorials and other information and got this script to work partially. The screen comes up but when you choose an image and click submit nothing happens can someone please take a look at this script. I am just learning but I have to learn quickly due to deadlines. I am running IIS on windows XP php 5.2; I know its basic but I am just trying to get a record to save then I will implement the safe guards. Any help would be very much appreciated! Robin <html> <head> <basefont face="Arial"> <title>File Upload To Database</title> </head> <body> <h3>Please Choose a File and click Submit</h3> <?php if (!isset($_POST['submit'])) { // form not submitted ?> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <input name="userfile[]" type="file" /> <input type="submit" value="Submit" /> </form> <?php } else { // form submitted // set server access variables $host = "localhost"; $user = "root"; $pass = "XXX"; $db = "YYY"; // get form input // check to make sure it's all there // escape input values for greater safety $imagetype = $_FILES["userfile"]["type"]; $imagename = $_FILES["userfile"]["name"]; ; $image = addslashes(file_get_contents($_FILES["userfile"]["tmp_name"])); $imagesize = $_FILES["userfile"]["size"]; $imagectgy = $_FILES["userfile"]["type"]; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "INSERT INTO images (image_type, image,image_size,image_ctgy,image_name) VALUES ('$imagetype', '$image','$imagesize','$imagectgy','$imagename')"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // print message with ID of inserted record echo "New record inserted with ID ".mysql_insert_id(); // close connection //mysql_close($connection); } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
JD* Posted August 20, 2008 Share Posted August 20, 2008 Change this $image = addslashes(file_get_contents($_FILES["userfile"]["tmp_name"])); to this $fp = fopen($_FILES['userfile']['tmp_name'], 'r'); $content = fread($fp, filesize($_FILES['userfile']['tmp_name'])); $image = base64_encode($content); This will get your content into the database. On display, make sure to use base64_decode or you'll just get gibberish Quote Link to comment 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.