Link to home
Start Free TrialLog in
Avatar of aymansoft
aymansoft

asked on

how upload files up 1mb in to mysql database?

hi all
how to upload files up 1mb such as 100mb and save this file in mysql database as binary field (BLOB)?

i try , but the form don't upload up 1mb in database

please see me any examples
i use this codes:
after submit :

            file_mime= $_FILES['userfile']['type'];
            $file_name= $_FILES['userfile']['name'];
            $file_size= $_FILES['userfile']['size'];
                  
if (!empty($file_name)){                  
                  
            $fp = fopen($_FILES['userfile']['tmp_name'], "rb");
            while(!feof($fp))
            {
            $file_contents .= fread($fp, 1024);  //fread is binary safe
            }
            fclose($fp);
            $file_contents = addslashes($file_contents);
            $file_contents = addcslashes($file_contents, "\0");

$sql="INSERT INTO email (user_send, user_recived, msg_title, msg_body, msg_date, file_mime, file_contents, file_name, file_size) VALUES ('$user_id', '$str1', '$msg_title', '$msg_body','$msg_date', '$file_mime', '$file_contents', '$file_name', $file_size)";
        $qq=mysql_query($sql);

i think the problem in apache ,

thanks for all
Avatar of richdiesal
richdiesal
Flag of United States of America image

What error are you getting that you're trying to diagnose?  Does the SQL query run fine, but the data is corrupt?  My instinct is that you're trying to put a 1MB+ file into a BLOB field, which is far too small to hold it (a BLOB only holds around 65k).  Try changing the BLOB to a MEDIUMBLOB (16MB) or LONGBLOB (4GB).  If that doesn't fix it, I need more information about your problem.
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aymansoft
aymansoft

ASKER

hi experts

i try the change value upload_max_filesize=128M

and i use largblob but i cannot to save file in database ,
I get this infinite warning message in the post page

Warning: feof(): supplied argument is not a valid stream resource in c:\appserv\www\localemail\newemail.php on line 142

Warning: fread(): supplied argument is not a valid stream resource in c:\appserv\www\localemail\newemail.php on line 144

please help me
Sounds like your script is trying to find the uploaded file at an incorrect location. Are you able to show us your code around lines 125-160 so we can see what is happening?
His script is posted above.

But another note: you also don't need to parse the file line by line in order to pass it to SQL.  Try changing the following lines:
while(!feof($fp))
            {
            $file_contents .= fread($fp, 1024);  //fread is binary safe
            }

To this:
$file_contents = addslashes(fread($fp, fileSize($_FILES['userfile']['tmp_name'])));

Otherwise, your code looks fine.  Are you sure that Apache has read access to the folder where temp files are stored?   Try to add "echo $_FILES['userfile']['tmp_name']l" in that script and see what the value is.
it print
C:\WINDOWS\system32\config\SYSTEM~1\LOCALS~1\Temp\php160.tmp
Let's double-check something.

Change:
$fp = fopen($_FILES['userfile']['tmp_name'], "rb");

to:
$fp = fopen($_FILES['userfile']['tmp_name'], "rb") or die("Could not open file.");

it open the tmp_name no error hear
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial