the_oliver Posted February 9, 2007 Share Posted February 9, 2007 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! Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
the_oliver Posted February 9, 2007 Author Share Posted February 9, 2007 File location idea is intresting, but i would like also to know how to store them strate. How would i go about this BLOB thing? Thanks Quote Link to comment Share on other sites More sharing options...
sspoke Posted February 9, 2007 Share Posted February 9, 2007 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.. Quote Link to comment Share on other sites More sharing options...
Adam Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 1. Both sspoke and MrAdam gave solutions similar to uploading the file and saving just the location in your database. 2. In order to store the actual data you'd fopen the uploaded file $_FILES['field_name']['tmp_name'], then fread the contents into a var, and insert it into the BLOB field. Quote Link to comment Share on other sites More sharing options...
camdagr81 Posted February 9, 2007 Share Posted February 9, 2007 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? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 9, 2007 Share Posted February 9, 2007 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. Quote Link to comment Share on other sites More sharing options...
camdagr81 Posted February 9, 2007 Share Posted February 9, 2007 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). Quote Link to comment Share on other sites More sharing options...
the_oliver Posted February 11, 2007 Author Share Posted February 11, 2007 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! Quote Link to comment Share on other sites More sharing options...
camdagr81 Posted February 14, 2007 Share Posted February 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.