Solved

how upload files up 1mb in to mysql database?

Posted on 2007-03-18
11
245 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

15 Experts available now in Live!

Get 1:1 Help Now