Solved

how upload files up 1mb in to mysql database?

Posted on 2007-03-18
11
242 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:aymansoft
  • 4
  • 3
  • 2
11 Comments
 
LVL 9

Expert Comment

by:richdiesal
ID: 18744423
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.
0
 
LVL 24

Accepted Solution

by:
glcummins earned 63 total points
ID: 18747559
Additionally, you need to make sure your server is configured to handle the upload of large files. Check your php.ini file for a line like:

upload_max_filesize = 2M

You will need to change this to fit your needs. Remember to restart your webserver after changing this value.
0
 
LVL 1

Author Comment

by:aymansoft
ID: 18751587
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
0
 
LVL 24

Expert Comment

by:glcummins
ID: 18751805
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?
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 9

Expert Comment

by:richdiesal
ID: 18752862
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.
0
 
LVL 1

Author Comment

by:aymansoft
ID: 18767509
it print
C:\WINDOWS\system32\config\SYSTEM~1\LOCALS~1\Temp\php160.tmp
0
 
LVL 9

Expert Comment

by:richdiesal
ID: 18767728
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.");

0
 
LVL 1

Author Comment

by:aymansoft
ID: 18774256
it open the tmp_name no error hear
0
 
LVL 9

Assisted Solution

by:richdiesal
richdiesal earned 62 total points
ID: 18775014
Did you try changing:
while(!feof($fp))
            {
            $file_contents .= fread($fp, 1024);  //fread is binary safe
            }

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now