Question

upload images in mysql

Asked by: CalmSoul

Hello,

I have a php application where we are uploading images in mysql. If images are more than 1MB script is failing...

TableImage::store failed - MySQL server has gone away SQL=INSERT INTO `images` ( `adid`,`image`,`published`,`ordering` ) VALUES ( '170','test_a8632','1','' )

This means the mySQL died while adding image row.

INSERT into images (adid,image,published,ordering) values ('174','This_i8690','1','1')

but the mysql seems to be overloaded and dies.

Please help me figure this issue? we have tried changing the php.ini but still no help.

Following values were changed in php.ini file.

post_max_size, upload_max_filesize
These to settings need to be changed
If the connection is slower max_execution_time = 180, memory_limit, post_max_size, upload_max_filesize




This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-05 at 06:45:19ID24874545
Topics

PHP Scripting Language

,

PHP and Databases

,

MySQL Server

Participating Experts
3
Points
500
Comments
11

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Uploading Larger files to MySQL
    I have installed PHP 4.3.9 and MySQL 4.1.7 on my local computer. I am editing the file 'my-medium.ini' file in MySQL so I can set the max_allowed_packet variable which should allow me to upload the larger files. I'm not sure which 'save' option to choose. I will be uploading ...
  2. php.ini upload settings
    Besides these php.ini settinga upload_max_filesize post_max_filesize max_input_time max_execution_time What other settings in the php.ini file would you recommend for uploading files larger than 1MB to a MySQL database ? What about timeout settings, persistent links ? I am g...
  3. no php.ini
    I use phpinfo it tell me that php.ini is in /tec but when i go to /etc no php.ini I use commnd find but not found . It mean that i not have php.ini or not Thank

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: gr8gonzoPosted on 2009-11-05 at 06:51:30ID: 25750015

Usually it's because there's a max size of the queries you can send to MySQL - are you trying to send all 1MB of queries to MySQL at the same time? Can you post the code you're using?

 

by: Ray_PaseurPosted on 2009-11-05 at 08:24:22ID: 25751071

Check the "common pitfalls" link here:
http://us3.php.net/manual/en/features.file-upload.php

Also, run this script and look at the values associated with upload file sizes.  phpinfo() will give you the effective values that are in use at the time the scripts are being run

<?php phpinfo(); ?>

                                              
1:

Select allOpen in new window

 

by: Ray_PaseurPosted on 2009-11-05 at 08:26:14ID: 25751089

Going forward (and probably not directly related to your issues here) you should consider storing the images in the server file system, not in the data base tables.  There are a host of reasons for this, but for now just trust that the right thing to have in the DB is a pointer to the image file, and not the image itself.

HTH, ~Ray

 

by: gr8gonzoPosted on 2009-11-05 at 08:40:37ID: 25751259

Just to be proactive, ther max size I referred to before is set by the max_allowed_packet variable on the MySQL server.

Your query didn't indicate that you were storing the image data inside the database itself, but if you are, then you should definitely listen to Ray's advice. While MySQL is -capable- of storing image data in the database, it's usually far better to store the files on the server and just store a "reference" to that file in the database.

 

by: RacekonijnPosted on 2009-11-06 at 01:31:27ID: 25757758

I think for small images, gr8gonzo and Ray_Paseur are correct. But if you have a lot of big images I think the best solutions is to store it in DB beceause of host space problems.

What i did for images and all other files is to convert the file to a base64 character stream and stores it as text in my database.
The problem then is to store a large string (say 4MB) into one field. Beceause it was not possible for me, I splitted the large string into several short strings and stored it in my database.
When someone wil download the picture/file, I get all the short string parts, combined these and decode the string from base64.

So you problem is maybe your database can't store such large objects into one field?

 

by: gr8gonzoPosted on 2009-11-06 at 04:09:48ID: 25758388

@Race,

I must disagree in several ways.

> beceause of host space problems.
Databases still take up space, and most hosts will still count the used space against your total space. Plus, base64-encoding the file only makes it larger (usually anywhere from 20% to 30% larger). If you don't believe me, try taking any file on your system and take note of its size. Then in PHP, do something like this:

<?php
print strlen(base64_encode(file_get_contents("/path/to/file")));
?>

The resulting number should be about 30% larger than the original number of bytes. So if the problem is space, then converting a 3 megabyte image might make it into a 4 megabyte string (wasting 1 megabyte), and then you're still saving it on a database that almost always (in shared hosting environments) lives on your same server.

Regarding your methods, you may want to re-think how you handle your own storage problem there. The way you have done it will not perform well with high traffic. I'll explain:

When you store the image data into the database, then in order to access it (and then display it), then you need to query the database for it, right? You have to do it each time that image is requested (unless you have a caching mechanism, but it sounds like you're trying to get around a different problem). When you pull data out of the database with a query, it's still a process of copying/transferring data from the database into the memory that PHP can access.

So if you think about it, every time a 3-megabyte  image is accessed from the database, you are downloading 4 megabytes of Base64 data (pieces at a time, which is even slower), then you are running a process to decode 4 megabytes of Base64 data (which can be very CPU intensive for that file size), and THEN sending 3 more megabytes of the final data to the visitor.

That is REALLY bad in terms of resource usage. You're using up a LOT of memory, a LOT of CPU, and a LOT of bandwidth, and you're not really gaining anything.

Imagine if you had a nice, big painting of your family. You could store it in your living room, where it's convenient to view. What you are doing is the equivalent of cutting up your painting into a bunch of pieces, then putting each piece into its own locked box and storing the boxes in your basement. Every time someone comes over and wants to see your family portrait, you have to go to the basement (the database), open all the boxes (the fields containing your data), and paste the painting back together carefully so it looks perfect (combining the fields and base64-decoding), and THEN show it to your visitor (the visitor downloading the image from their browser).

You're claiming you're doing this because you have no space in your house, but in reality, the basement is probably still part of your house. It's not like you have a basement that has unlimited space - it's just a different part of your house space (your hosting space), and all the different lock boxes actually take up a little more room than the original painting did, so you're losing more space, and you're doing a lot more work to show the painting than what really needs to be done.

Disk space is the cheapest part of web hosting today - if you tell us what host you are using where they do not count database space and have some really low limit with disk space, I'm 110% sure that I could probably recommend a better host at the same price (unless you're getting this for free or are on some $1/month host that gives you no support or anything).

It doesn't matter if your file is small or large, there are very, very few times where storing a file into a database is ever useful. I have done it a few times on projects where I thought it would be easier for one way or another (e.g. portability so I could move the database), but every single time I've done it, I have regretted it later and seen that storing it on the filesystem would have been much better and faster (ESPECIALLY with smaller files, since those can be cached more easily by the web server and thus delivered to the visitor EXTREMELY fast).

That said, please do not take this as criticism of YOU. This is all meant to be constructive criticism, and I am trying to steer you away from a mistake that MANY people have made.

And, while I don't want to encourage the use of MySQL to store image data, I should point out that there IS an appropriate way to store it without base64-encoding and without splitting it into small strings. The BLOB fields (e.g. BLOB, LONGBLOB, etc...) can hold raw data. By using hex-encoding, you can tell the database to store the raw data as-is:

$data = file_get_contents("myfile.jpg");
$arrData = unpack("H*hex", $data);
$hex = "0x" . $arrData["hex"];

INSERT INTO (blobfield) VALUES ($hex);

Of course, this way is still limited by the max_allowed_packet setting in MySQL, so if you absolutely MUST use this method, then you would have to get your host to increase that setting to something that would be larger than the file size you're uploading (if you're uploading 4-megabyte files, then have it increased to 6 megabytes, just in case you come across a a slightly-larger file - it won't hurt to have a slightly-larger limit )

 

by: RacekonijnPosted on 2009-11-06 at 04:18:56ID: 25758441

i agreed its not a great solution and i do not used that system for images but for files (*.zip *.pdf, ...).
The reason i did this is beceause i have only 50MB on disk space on my host and no database limit or badwith limit, so this was the only method to solve it.
But you are right about the base64 encoding, that part is not nessecary, i could also splitted the blob into several parts, and save some database disk space so.


 

by: gr8gonzoPosted on 2009-11-06 at 04:20:49ID: 25758457

50 megabytes of disk space? That is REALLY low by today's standards. Who is your host?

 

by: RacekonijnPosted on 2009-11-06 at 04:24:08ID: 25758476

Priorweb from belgium http://www.priorweb.be/hosting/web/economy
but it's quite old already, have it now for 7 years or something and they never changed it.

 

by: gr8gonzoPosted on 2009-11-06 at 04:32:19ID: 25758530

Wow! 75 euros, too. You should really look for a different host. I'm not familiar with Belgium-specific hosts, but given the exchange rate ($112 US), that's ALMOST like paying for this plan on a month-to-month basis:

http://www.jaguarpc.com/shared-hosting/

(paying in advance would make it cheaper than the 75 euros you're paying now). And that one gives you unlimited disk space.

I'm not saying you should go with JaguarPC, but it should definitely illustrate that there are some better hosts out there for the same price or cheaper.
...

 

by: Ray_PaseurPosted on 2009-11-06 at 05:41:18ID: 25758929

"if you have a lot of big images I think the best solutions is to store it in DB"

This advice is so wrong that it makes me sad to see it here.  There are a panoply of reasons why you do not want to do something like this, starting with performance, backup, and ease-of-use.

I think you might want to look worldwide for your next hosting plan.  It's not uncommon to find a plan on a REALLY GOOD shared hosting company in USA for only a few dollar a month.  Consider DreamHost, HostGator and my favorite service, ChiHost.  Avoid GoDaddy unless their tech support has gotten better over the last month.

Gotta move on.  Over and out, ~Ray

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...