473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how do I store a binary file in a MySql database, as a string?

I'm a little weak on my basic I/O. Help me out please.

Is it right to say that I can just open any file with file(), get it as
a string, and then store in a MySql database, in, say, a MediumText
field? I realize that MySql supports binary fields, but my current
MySql schema does not have any binary fields, and I'm wondering if I
can store the file without changing my current schema.

Can I recreate the file later, if I wish, just be grabbing the string
out of the database and writing it to disk? (on a Linux system)

May 18 '06 #1
5 3345
lawrence k wrote:
I'm a little weak on my basic I/O. Help me out please.

Is it right to say that I can just open any file with file(), get it as
a string, and then store in a MySql database, in, say, a MediumText
field? I realize that MySql supports binary fields, but my current
MySql schema does not have any binary fields, and I'm wondering if I
can store the file without changing my current schema.

Can I recreate the file later, if I wish, just be grabbing the string
out of the database and writing it to disk? (on a Linux system)


Yes.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 18 '06 #2
"lawrence k" <lk******@geocities.com> writes:
I'm a little weak on my basic I/O. Help me out please.

Is it right to say that I can just open any file with file(), get it as
a string, and then store in a MySql database, in, say, a MediumText
field? I realize that MySql supports binary fields, but my current
MySql schema does not have any binary fields, and I'm wondering if I
can store the file without changing my current schema.

Can I recreate the file later, if I wish, just be grabbing the string
out of the database and writing it to disk? (on a Linux system)


AFAIK you need to use blob fields as your data is binary (or is it,
what kind of files do you want to open?), if you store
it in a normal text field it can be corrupted. But else you are right,
you just take the ourput from the field and put in a file or output to
the browser with a header infront.
--
Henrik Hansen
May 18 '06 #3
Carved in mystic runes upon the very living rock, the last words of
Henrik Hansen of comp.lang.php make plain:
"lawrence k" <lk******@geocities.com> writes:
Is it right to say that I can just open any file with file(), get it
as a string, and then store in a MySql database, in, say, a
MediumText field? I realize that MySql supports binary fields, but my
current MySql schema does not have any binary fields, and I'm
wondering if I can store the file without changing my current schema.


AFAIK you need to use blob fields as your data is binary (or is it,
what kind of files do you want to open?), if you store
it in a normal text field it can be corrupted. But else you are right,
you just take the ourput from the field and put in a file or output to
the browser with a header infront.


You could base64_encode() it first.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 18 '06 #4
Alan Little <al**@n-o-s-p-a-m-phorm.com> writes:
Carved in mystic runes upon the very living rock, the last words of
Henrik Hansen of comp.lang.php make plain:
"lawrence k" <lk******@geocities.com> writes:
Is it right to say that I can just open any file with file(), get it
as a string, and then store in a MySql database, in, say, a
MediumText field? I realize that MySql supports binary fields, but my
current MySql schema does not have any binary fields, and I'm
wondering if I can store the file without changing my current schema.


AFAIK you need to use blob fields as your data is binary (or is it,
what kind of files do you want to open?), if you store
it in a normal text field it can be corrupted. But else you are right,
you just take the ourput from the field and put in a file or output to
the browser with a header infront.


You could base64_encode() it first.


But then you have to keep in mind the content will take up much more
space.

Just tested, a 91959 bytes files will become 122612 bytes, that will end up
being alot if you have lots of files..

But it's a working solution if you cant convert to a BLOB (can't see
why not though).

--
Henrik Hansen
May 18 '06 #5

Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Henrik Hansen of comp.lang.php make plain:
"lawrence k" <lk******@geocities.com> writes:
Is it right to say that I can just open any file with file(), get it
as a string, and then store in a MySql database, in, say, a
MediumText field? I realize that MySql supports binary fields, but my
current MySql schema does not have any binary fields, and I'm
wondering if I can store the file without changing my current schema.


AFAIK you need to use blob fields as your data is binary (or is it,
what kind of files do you want to open?), if you store
it in a normal text field it can be corrupted. But else you are right,
you just take the ourput from the field and put in a file or output to
the browser with a header infront.


You could base64_encode() it first.


Thanks much to the both of you. I've MP3s to store. I will encode them
with base64_encode() and store them in a mediumtext field. We will see
how this goes.

May 22 '06 #6

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

Similar topics

5
by: Lars Behrens | last post by:
Hi there! For a web project I need a little expert help. I don't have written much code yet, just been fiddling around a bit, testing and planning. The web site will have a submission page for...
3
by: David Stockwell | last post by:
Hi, I'd like to read the contents of a file into memory. The problem is that this file is binary. I then want to store the whole thing in memory to a database as a blob. I have no problem...
0
by: Ivan Horvath | last post by:
Dear All, i would like to store binary file contents to a mysql database in a mediumblob fields. i've got problem when there is a simple quote, double quot, backslash or NUL character in the...
12
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create...
0
by: Gert | last post by:
Hi, I need to store a array() of byte to a mysql field. I created a field in mysql of type varbinary. But storing and retrieving gives problems. Please help. The strTemp = EnDeCrypt(txt,...
3
by: Me Alone | last post by:
Hello: I am trying to edit some C code I found in "The definitive guide to using, programming, and administering MySQL" by Paul DuBois. This C client program connects and then segfaults when...
4
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con =...
3
by: M.-A. Lemburg | last post by:
On 2008-08-07 20:41, Laszlo Nagy wrote: 1 It also very fast at dumping/loading lists, tuples, dictionaries, floats, etc. -- Marc-Andre Lemburg eGenix.com
7
by: andyehi | last post by:
Hi Everyone, I am having a txt file config.txt on application start up path.I need to read that file line by line using a delimiter ; or , and store the values in a variable to connect to the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.