devofash Posted March 10, 2010 Share Posted March 10, 2010 Hi Guys, Got a small issue. Am trying to upload files to mysql database. Works perfectly. Except for one small problem when downloading the image files are corrupted. Was wondering if someone could help me fix this. I have to have to store them in the database unfortunately. the field type is blob and have also tried text but same problems //upload.php if($_FILES['logo']['size'] > 0) { $filename = cleanFilename($_FILES['logo']['name']); $tempname = $_FILES['logo']['tmp_name']; $filesize = $_FILES['logo']['size']; $filetype = $_FILES['logo']['type']; $fp = fopen($tempname, 'r'); $content = fread($fp, filesize($tempname)); $filecontent = addslashes($content); fclose($fp); } // insert statement //download.php //sql statement .... $filename = $row['filename']; $filetype = $row['filetype']; $filesize = $row['filesize']; $filecontent = $row['filecontent']; header("Content-type:$filetype"); header("Content-length:$filesize"); header("Content-Disposition:attachment;filename=$filename"); echo $filecontent; exit(); PDF files work its just the image files that get corrupted. much appreciate the assistance. Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2010 Share Posted March 10, 2010 Use a phpinfo() statement to check if the magic_quotes_runtime setting is ON. If it is, it is causing the problem because the data being read from the file is being escaped and the data being retrieved from the query is also being escaped. You can unconditionally just turn off magic_quotes_runtime at the start of your script. Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024203 Share on other sites More sharing options...
devofash Posted March 10, 2010 Author Share Posted March 10, 2010 its off, what can I do now? magic_quotes_gpc Off Off magic_quotes_runtime Off Off Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024228 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2010 Share Posted March 10, 2010 when downloading the image files are corrupted Define: corrupted. Exactly what are you getting that leads you to this conclusion? Short-answer, you need to investigate at what point your code is working and at what point it is not. We don't have access to your server, your code (except for what you do post), or your database. You are the only one here who can troubleshoot what is going on. Have you opened the downloaded file using a programming editor to see exactly what is in it? Is it of the expected size? Have you checked if something is actually stored in the database? Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024232 Share on other sites More sharing options...
devofash Posted March 10, 2010 Author Share Posted March 10, 2010 By corrupted I mean it cuts it off half way so for example if I upload an image 600px by 900px it displays say the first 100px of the file and the rest is greyed. The original filesize is in this case 339 KB and when I download it's 63.9 KB. In the database its 339KB. I've checked the database it stores the stuff as binary. I've opened it via PSPad and I see lots of binary stuff. Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024240 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2010 Share Posted March 10, 2010 So, investigate more at what point your code is or is not working and under what conditions it fails. Is the length of the data being retrieved by the query correct? I would use strlen Does this work for a smaller file? What method are you using to query and retrieve the data from the database? We have seen some of the database types have problems with specific size data due to bugs features in the database drivers. Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024248 Share on other sites More sharing options...
devofash Posted March 10, 2010 Author Share Posted March 10, 2010 hmm it works on smaller files so for example if i upload a logo 100x100 it works perfectly. checked the filecontent with strlen and it shows as 65535 even though the filesize in the database 347715 i've basically followed the steps from the tutorial http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024257 Share on other sites More sharing options...
devofash Posted March 10, 2010 Author Share Posted March 10, 2010 oooh.... i changed the data type to mediumblob and it works =) but didn't work when i did it with blob why ?? anyways awesome ... thanks for your help PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024262 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2010 Share Posted March 10, 2010 BLOB[(M)] A BLOB column with a maximum length of 65,535 (216 – 1) bytes MEDIUMBLOB A BLOB column with a maximum length of 16,777,215 (224 – 1) bytes Quote Link to comment https://forums.phpfreaks.com/topic/194776-uploading-file-into-mysql/#findComment-1024267 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.