twistisking Posted May 12, 2007 Share Posted May 12, 2007 I thought this code was perfect but unfortunately, every time I attempt to upload an image, it will only upload 1 kb of data. Can anyone tell me what Im doing wrong? <?php $con = mysql_connect("host","login","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("Database", $con); function getimage($file){ $takeFile = fopen($file, "r"); $file = fread($takeFile, filesize($file)); fclose($takeFile); return $file; } function getfileType( $name ){ $name = explode(".", $name); $name = array_reverse($name); $name = strtolower($name[0]); return $name; } $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); $allowedImageTypes = array("jpg"); if(empty($_FILES['image']['tmp_name'])){ die("File not uploaded"); } else { $fileType = $_FILES['image']['name']; if(in_array(getfileType($fileType), $allowedImageTypes)){ $fileContent = getimage($_FILES['imgfile']['tmp_name']); $sql="INSERT INTO post (poster, poster2, title, category, tagline, thearticle, image) VALUES ('$_POST[poster]','$_POST[poster2]','$_POST[title]','$_POST[category]','$_POST[tagline]','$_POST[thearticle]','$content ')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } if(mysql_affected_rows() > 0){ die("Image inserted successfully"); } else { die("Image can not be inserted check your submission"); } mysql_close($con); } else { die("This is not a valid image type"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/ Share on other sites More sharing options...
StormTheGates Posted May 12, 2007 Share Posted May 12, 2007 Have you checked your PHP.ini and mysql settings to see the maxium upload settings? And correct me if I am wrong but can images even be uploaded to a MYSQL db? Maybe a link to it but not the actual code of the image itself :S Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-251060 Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 And correct me if I am wrong but can images even be uploaded to a MYSQL db? Maybe a link to it but not the actual code of the image itself :S You can upload the Image's Binary data into Database. But its not fair and also it will increase your work load . what you need to do is just to insert the location of that Image in MySQL Database and then Use that. Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-251137 Share on other sites More sharing options...
twistisking Posted May 12, 2007 Author Share Posted May 12, 2007 And correct me if I am wrong but can images even be uploaded to a MYSQL db? Maybe a link to it but not the actual code of the image itself :S You can upload the Image's Binary data into Database. But its not fair and also it will increase your work load . what you need to do is just to insert the location of that Image in MySQL Database and then Use that. Thanks for the advice but all i want to do is upload the image's binary data. I already created a script the reverts the binary data back to a normal image. I can upload the binary data using phpmyadmin but for security reason of course I don't wana everyone being able to access that. Also I dont want people just inserting images on others servers, just incase that server deletes their image. Just too many problems i could run into. Anyone else have any advice to get all the image data into mysql? Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-251446 Share on other sites More sharing options...
neel_basu Posted May 12, 2007 Share Posted May 12, 2007 Look if you fear about security of your Images there is still Ways . You can still keep secure just Keeping the Locations into Database. You have to Keep a .htaccess file into that folder where you keep your image file Deny from All In the .htaccess file Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-251449 Share on other sites More sharing options...
chronister Posted May 12, 2007 Share Posted May 12, 2007 If you store the actual binary file in the database, you will take a large performance hit. It can be done, but it would be better to store the path and information about the image, and actually store the image in a directory on the server. For security, to keep people from being able to access the folder directly just do what neel_basu suggested. It is a lot quicker to grab a several hundred bytes containing image information and use the information to link to the directory and to the file itself, rather than trying to grab that info AND a 300-500Kb file. Unless you put constraints on how big the file you could be trying to grab database info in the MB+ range and that would put a huge load on the server if your site is fairly busy, not to mention the size of the database could be HUGE. My hosting provider gives me something like 50 databases at 100MB each. That could get filled up very quickly if I were to store images and such. but storing text and numbers I can store TONS of data Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-251476 Share on other sites More sharing options...
twistisking Posted May 15, 2007 Author Share Posted May 15, 2007 Okay I have been convinced! thank you all for the help! Quote Link to comment https://forums.phpfreaks.com/topic/51022-solved-upload-image-to-mysql-with-php/#findComment-253613 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.