Upload and display picture on database with php

hi i need source code for upload pic on db and diplay on page…i searched from morning but no luck…every source had its own problem,

can you help me?

Searched this forum? I know there must be two or three examples here at least.

…every source had its own problem,.

What does this mean?
Does not work or does not work how you want it to.

The idea of the forum is to help people with their code not just give them some code.

Do you know how to enter things into a database if so you can write that part - I would just store the path to the image.
Can you write a form if so all you need to do is specify an image.

If you can do the above all you need to do is combine them and you are most of the way there.


You do not store pictures in databases, just the image info. Where it is stored is a matter for you to configure your coding so the right image is displayed from where it is stored (or linked)

You are going to cause your database table to query very, very slowly if you store images inside a database, i.e. not recommended

1 Like

You can store images in a database BUT and it is for very good reason that YOU DON’T store any images in a database, the main reason is the impact it has on your server to operate and retrieve what is meant to be short blocks of data.

As you have been advised, you store the URL / File path to the file and serve it up as an HTML <img tag reference,

If you are wanting to do this because you want to try it, theres no harm in knowing BUT and it is just that, as previously explained, to do so in any number or on a relatively small site, your user traffic without a doubt when it reaches a threshold, the database will keep on flat-lining.

The Steps…

  1. fread() the file in to a variable
  2. base64_encode the data stream in to a string
  3. pre-append the string with the file type and if needed encode
  4. serve up as a string directly in to an appropriate display object like an <img tag

Good Luck and please, don’t do it for a live development, theres very good reasons why you don’t do this.

The above method is an adaptation of how to .mht your web pages,

1 Like

so thanks to all of you… i can insert image on database but i cant display this…i follow step by step in tutorials in net but no luck…and i found a tutorial tahat save image on directory and save image link on db…as you all told me i think its beter work to do.


There are also very good reasons sometimes FOR storing a copy of the actual image in the database such as if it is a part of a transaction.

The bigger the system the more likely that you will need to store the actual images in the database even if you also keep copies outside of the db for efficiency of access.

Only the simplest of systems can get away with only having a copy of the image outside of the database.

One page that provides a tutorial on how you can store images in the database and which also mentions a lot of the reasons why it might be a good idea to do so is http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

1 Like

yeah i found this one before,it was so awsome tutor but in pdo…i programing my project with mysqli,

i dont know i can use pdo on there too?

There is very little difference between using PDO and using mysqli for doing this.

The only change I’d make would be to use file_get_contents($image[‘tmp_name’]) to pass the actual file content for the image rather than a file pointer as I don’t think mysqli supports passing file pointers.

1 Like

I disagree in some part, IF you MUST store a sales reference, you only need to keep the data in a record in a database and issue the consumer with a PDF receipt that contains the image generated via PHP and then either emailed to the consumer or made available as a download.

Why use +300Kb in a data-string in a database Vs a few hundred bytes to constitute the same information, in big applications, this would constitute a burden on the database because of its size would be getting unmanageable.

If you pay for data storage or have a cap, this becomes a problem because of limits and rising costs, so from a business perspective, it is a poor choice for retaining information.

As long as your database is robust and encrypted, you have no need for retention of images in any database.

1 Like

yes good point,but i mixed up more than before.

i think storing image in databse or not is up to our progect!

so you mean i save picture on directory and place its link on databse for showing.yeah,i readed in book of O’Reily Publishing About PHP&MYSQL that for storing image they used directory,not database,i think its good point,

The book https://pragprog.com/book/bksqla/sql-antipatterns states that storing images in a directory is wrong and that they should be stored in the database. It then discusses a few cases where you may want it in a directory and be prepared to handle the extra overhead that involves.

Prior discussions on this forum have mostly ended up concluding that storing in both places overcomes almost all of the disadvantages of either approach.

Of course if the image is being generated online in the first place there is no need to store it in a database as you only need to store the info required to allow you to regenerate it.

1 Like

Well someone seems to have forgotten what webservers were designed for, delivery of large chunks of data.

Databases are suited to small chunks of data and bloating one out with images is a mistake. Folders are more manageable and keeping data hidden is only a matter of setting folder permissions.

I know I prefer to go with small compact and streamlined to keep speeds up and thats what counts, load, the load this puts on PHP and MySQL or whatever databasing you decide on and that compared to delivery of a path to data by a web server…

Your bottleneck will be the parsing of information and interaction by PHP with the database server and then handle the response and process the data and then deliver that data to the browser (endpoint)

Verses URL request processed by webserver (no bottleneck as it is its native function) and then getting redirected straight to the resource (path)

Which one do you thing is faster and has less loading?

1 Like

Databases are actually just a file system with an efficient search and management mechanism overlaying it.

When you rollback a transaction that is entirely within the database everything gets restored back including the images but if you rollback when the image itself is outside of the database then there is nothing to undelete the image.

If folders were more manageable than databases then databases would never have been created since all databases do is to manage folders and files.

the database may be slightly slower for transactions that are committed but not using the database is definitely a lot slower for transactions that are rolled back (perhaps weeks instead of seconds).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.