twilitegxa Posted September 22, 2008 Share Posted September 22, 2008 I have this code, but when I try to test it to upload an image into the table, it just says File not uploaded. Can anyone help me figure out why? Here is my code. <?php //connect to database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("smrpg",$conn) or die(mysql_error()); /* This function take the image from form variables */ function getImageFile($file){ $takeFile = fopen($file, "r"); $file = fread($takeFile, filesize($file)); fclose($takeFile); return $file; } /* We learn image type using this function Because we will let only gif, jpg and png images can be uploaded */ function getfileType( $name ){ $name = explode(".", $name); $name = array_reverse($name); $name = $name[0]; return $name; } $allowedImageTypes = array("gif","jpg","png"); if(empty($_FILES['image_file']['tmp_name'])){ echo "File not uploaded"; } else { $fileType = $_FILES['image_file']['name']; if(in_array(getfileType($fileType), $allowedImageTypes)){ $fileContent = getImageFile($_FILES['imgFile']['tmp_name']); $uploadedImage = chunk_split(base64_encode($fileContent)); $query = "INSERT INTO images_table VALUES('NULL','$imgName','$uploadedImage')"; $result = mysql_query($query); if(mysql_affected_rows() > 0){ echo "Image has been inserted succesfully"; } else { echo "Image can not be inserted. Check your submission."; } } else { echo "This is not a true image type. Image must be a jpg, gif, or png."; } } ?> And here's the form to submit the code: <html> <head> <title>Upload Image</title> </head> <body> <form action="imageupload.php" method="POST" enctype="multipart/form-data"> Name : <input type="text" name="imageName"> <br/> Image :<input type="file" name="imageFile"> <input type="submit" value="Upload" name="func"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
dropfaith Posted September 22, 2008 Share Posted September 22, 2008 where is it defining the name of the input from form it seems it should be in this i could be way off but isnt $file supposed to be $imageFile based on the name in the form function getImageFile($file){ $takeFile = fopen($file, "r"); $file = fread($takeFile, filesize($file)); fclose($takeFile); return $file; } Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 22, 2008 Author Share Posted September 22, 2008 I tried changing all the $file variables to $imageFile, but that didn't fix the problem. Any other suggestions? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted September 24, 2008 Author Share Posted September 24, 2008 Do you guys need any other information to see the problem, like what fields are in the table in the database or anything like that? 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.