473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing files on a database?

I wrote this script to display different files from a database and
properly display them by their respective MIME types. I have two
questions: the first is that it just so happens not to work, and I'm
just wondering if there are any discrepancies that I can't see that
maybe someone else can, and the second is what would be the benefits
and the drawbacks of storing different types of files in a database,
for example files like jpeg and gif images, flash files, and mpeg
videos?

Code:
<?php
error_reporting (E_ALL);
require 'config.php';
$id = $_GET['id'];
$dbTable = $_GET['tb'];
dbConnect();
$query = "SELECT content, mime_type FROM ".$dbTable. " WHERE id=".$id;
$result = mysql_query("$q uery")
or die("Invalid query: " . mysql_error());
$data = mysql_fetch_arr ay($result);
header("Content-type: ".stripslashes( $data['mime_type']));
echo stripslashes($d ata['content']);
dbDisconnect();
?>

Dec 10 '06 #1
3 1903

Lucky_Syringe wrote:
dbConnect();
are you passing any parameters?
or calling mysql_select_db ('my_db'); ?

Dec 11 '06 #2
BKDotCom wrote:
Lucky_Syringe wrote:
dbConnect();

are you passing any parameters?
or calling mysql_select_db ('my_db'); ?
Why yes, yes I am...

Dec 11 '06 #3
On 9 Dec 2006 20:52:37 -0800, "Lucky_Syri nge" <ke**********@g mail.comwrote:
>I wrote this script to display different files from a database and
properly display them by their respective MIME types. I have two
questions: the first is that it just so happens not to work,
In what way does it not work?
>and I'm
just wondering if there are any discrepancies that I can't see that
maybe someone else can,
See below.
>and the second is what would be the benefits
and the drawbacks of storing different types of files in a database,
for example files like jpeg and gif images, flash files, and mpeg
videos?
Putting it in the database means the data will be easier to keep consistent
with its own metadata, and reduces some of the worries about inconsistencies
introduced by failures between the commit of the database changes, and changes
to a filesystem (where you can't just do a rollback). It can make it easier to
back up, or at least easier to restore to a consistent point.

But it means you have a lot more data in your database, which some databases
may not be able to handle well. Your database may not have good large object
support (MySQL certainly doesn't - there's no API to stream the contents out,
so fetching a BLOB ends up with the whole file copied from place to place in
memory potentially several times). It's also much faster to serve files off the
filesystem (although this can be mitigated by having a disposable filesystem
cache of the files, but keeping the database copy as the master).

The method to choose depends on lots of factors.
>Code:
<?php
error_reportin g(E_ALL);
require 'config.php';
$id = $_GET['id'];
$dbTable = $_GET['tb'];
dbConnect();
$query = "SELECT content, mime_type FROM ".$dbTable. " WHERE id=".$id;
Potential for a SQL injection attack - use of user input without validation.
>$result = mysql_query("$q uery")
or die("Invalid query: " . mysql_error());
$data = mysql_fetch_arr ay($result);
header("Conten t-type: ".stripslashes( $data['mime_type']));
Why the stripslashes here?
>echo stripslashes($d ata['content']);
And particularly here - this is likely to corrupt the file.
>dbDisconnect() ;
?>
--
Andy Hassall :: an**@andyh.co.u k :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5890
by: Bola | last post by:
Is anyone know how I can store Word Docs and PDF's in database, and how I can make link for visitor to download these files
3
2947
by: Dave Smithz | last post by:
Hi There, Being quite new to MS-SQL I would like to ask if there is a general opinion of what approach should be taken to storing things like external documents and images in databases. Should the actual files be stored within the database, or instead should links to the files on a file server or something similar be stored instead. For the end user I imagine it is easier to have everything stored within the
5
2145
by: Don Vaillancourt | last post by:
I'm building a system when one can upload a document to the website. I will be storing the document on the hard-drive for quick/easy access, but I was also thinking of storing it in an existing database since most of the sites information is all stored there. As well there would be only one place to worry about backing up. And if the file on the hard-drive was ever missing or became corrupted, I could restore it form tha database. Is...
6
3044
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a folder on the server and then in the database table that I INSERT the news article, I'll store the path of the uploaded image. To me this seems a bad idea as if the image paths were changed on the
6
7716
by: stenospamron | last post by:
Does anyone have any advice as to how to get JPG images into an OLE Object field? I have created a table that includes this data type, and allowed Access wizzard to generate a form. I wish to copy various images from the web (in my initial trials, I copied the files to my PC), and which generally seem to be JPG files. However, Access (Access 2000 ver 9.0.2720) does not seem to be able to cope with this. When I attempt to insert the...
7
2788
by: C G | last post by:
Dear All, What's the best way to store jpgs in postgresql to use in a web page? I tried to use large objects, but how would you extract them from a table to be viewed in a web-page without having to write them to a scratch file somewhere first? Thanks
4
4640
by: IkBenHet | last post by:
Hello, I am working on an ASP.NET based website project. Without going into much detail; One of the function on this website will be the possibility to upload images via a ASP.NET form to the webserver. These images can then be shown on to webpages throughout the site. What would be the best way of storing these images? Just by saving them in a folder on de webserver or in a database?
9
2373
by: Adam J Knight | last post by:
Hi all, Just wondering whats everyones prefered method of storing images ? 1) File System 2) Database (SqlServer) (Seems to be easier, but has a performance hit) Appreciate some insight!!! Cheers,
6
3198
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to me like there were performance issues at the time. How about the different types? The MS docs I would expect Access to differentiate and handle appropriately (i.e. .DOC and .XLS).. but how about ..PDF? and can I stash a .TXT document in the...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.