Jump to content

Uploading file into mysql


devofash

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.