Jump to content

Files in databases


the_oliver

Recommended Posts

Hi,

 

Was wondering if some one explain how put files into a database, and then, for something like images, use them!

Database i am using in postgreSQL.

 

Can they just be inserted into a standard table coloumb and thus search like anything else?

 

Many Thanks!

Link to comment
Share on other sites

I honestly think that inserting files themselves into a database is a horrible practice. You should really upload them to a location, and save the location in the database.

 

You would have to upload them into a BLOB if you really want to store them in a database. As for displaying an image, you'd probably have to make a php page to retrieve it, echo a Content-Type of that image, then echo the data.

Link to comment
Share on other sites

you just have to store the filename maybe full link to image but the image it self just upload it to some folder on your hosting using any open source upload script just add sql support to record filenames and paths unless you only use 1 folder than you don't need to know the paths since they never change..  but I could be wrong what you want..

Link to comment
Share on other sites

For a website I recently made, I created a file called "image.php" and used a parameter ("url") to locate the image.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
<title>Image: <?php print $_GET['url']; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-image: url('images/img-bk-cross.gif');
margin: 0px;
}
</style>
</head>
<body>
<img src="<?php print $_GET['url']; ?>" alt="" />
</body>
</html>

 

I used this for an "enlarge image" type of thing, and because the company had all their product images the same size, I used javascript to size the window. Looked much better than having a lot of whitespace in the window.

 

If you wanted, you could make it more adavanced by adding captions and titles. To do this, you could either assign an id to the image, then use a parameter like: "id=34" to identify the url, and caption; titles anythign else you'd want. OR, you could use the image path from the root (eg. images/catalog/34.jpg).

 

- Obviouslly the captions and titles would be stored in a database.

 

If you are interested in storing the ACTUAL image in the database, it would have to be some kind of binary or encryption used.

Link to comment
Share on other sites

I honestly think that inserting files themselves into a database is a horrible practice. You should really upload them to a location, and save the location in the database.

 

You would have to upload them into a BLOB if you really want to store them in a database. As for displaying an image, you'd probably have to make a php page to retrieve it, echo a Content-Type of that image, then echo the data.

 

Loading files into a database is a great idea, it takes less space on the server (especially if you're skilled enough to gzip the file at runtime).

 

Why are you against it?

Link to comment
Share on other sites

It's less effort for the server to load an image from the filesystem then get all that data from the database and reconstruct the image each time. The filesystem is made for files, that's what an image is. Yes, an image has data, but it's not the type of data that databases are made for.

 

I think it's mostly personal preference.

 

Link to comment
Share on other sites

I honestly think that inserting files themselves into a database is a horrible practice. You should really upload them to a location, and save the location in the database.

 

You would have to upload them into a BLOB if you really want to store them in a database. As for displaying an image, you'd probably have to make a php page to retrieve it, echo a Content-Type of that image, then echo the data.

 

Loading files into a database is a great idea, it takes less space on the server (especially if you're skilled enough to gzip the file at runtime).

 

Why are you against it?

 

How exactly does it take up less space on the server? It takes up the same amount of space within the database file as it does to save it elsewhere, as well as the minor space that will be taken up by declaring a new field. You can gzip a file no matter which path you take.

 

As well, storing it there makes it harder to manipulate the data externally. That is, if you store an image I can manipulate it easier by saving it elsewhere then saving that data.

 

Also, transferring that data between your database server and your PHP parser will have a serious effect on your site, where as pulling a small string (less then 255 characters) takes much less time, and you can easily echo it out again.

Link to comment
Share on other sites

It would really depend on the instance and use of the file. If the file is an image, sure, it's probably better to just store the file locally, but if the file is mirrored, or needed on multiple hosts, I would think storing it in the db would be more practical.

 

A lot of hosting packages offer unlimited db yet limited file storage. Use the db space instead of your precious script space ;) It's also easier and faster to back up/export a db than it is to move an entire directory from one place to another (which i've learned the hard way when a host i was using decided to pack up and call it quits).

Link to comment
Share on other sites

I like this point about multiple sites using using it. Not haveing to make a file public just a table.  But can also set the point about speed and server load!  I think camdagr81's point could be intresting from a development point of view also.... Not having to wory about changing file locations etc... especialy if files are out side the web file point.

 

Im realy just wondering how it would be done with a database!  How do i go about putting a file into a 'BLOB' field?

 

Thanks for all your coments so far!

Link to comment
Share on other sites

Read the fread() function in the php manual. That will show you how to read the file contents. After understanding that, you would just insert the fread() data into a field in your table, just as if you were inserting regular text or numbers. The fields property has to be a blob so that the data doesn't get encoded. You will also want to specify some data about the file in the same node; such as: file name, file type, creation date, etc..

 

When you need to access the file you use fwrite(), dumping the contents of the blob field into a file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.