/    Sign up×
Community /Pin to ProfileBookmark

inserting doc files in mySQL

Hello..
I was wondering did anyone tried this so far?? I have set my database and I have PHP script that can display doc files from directory I would like to this in DB and to use mysql..

code is:

<?php
$dir_if_any = ‘E:/path/toDir/’;
echo ‘<table align=”center” width=”100%” bgcolor=”#666666″ cellspacing=”1″ cellpadding=”5″>’;
echo ‘<tr><td bgcolor=”#FFCC00″ colspan=””>reports</td></tr>’;
$file_types_allowed = array( ‘doc’);
foreach ($file_types_allowed as $var) {
foreach (glob(“$dir_if_any*.$var”) as $file_info_1) {
if($rownum == ‘0’) {
$rowbgcolor = ‘#CCCCCC’;
$rownum = ‘1’;
} else {
$rowbgcolor = ‘#AEAEAE’;
$rownum = ‘0’;
}
echo ‘<tr><td bgcolor=”‘.$rowbgcolor.'”><A href=”‘.$file_info_1.'”>’.$file_info_1.'</a></td></tr>’;
// echo ‘<tr><td bgcolor=”‘.$rowbgcolor.'”><A href=”‘.$file_info_1.'”>’.$file_info_1. filesize($file_info_1).'</a></td></tr>’;
}
}
echo ‘</table>’;
?>

any help is very very welcome…

Regards,

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@themartyJun 08.2006 — Just insert the contents (obtain them with [url=http://www.php.net/file_get_contents]file_get_contents()[/url]) of the document in mysql, like you would insert any other string.

The field-type you would use for this in mysql is LONGBLOB, make sure it is of that type, otherwise it won't work.
Copy linkTweet thisAlerts:
@amonamonauthorJun 08.2006 — hmmm I do not think that is solution..I tried this:

<?php

$file = file_get_contents ('dok.doc');

echo $file;

?>

and I get stupid characters...but it is perfect for this:


<?php

$file = file_get_contents ('dok.txt');

echo $file;

?>

for txt documetns it works perfect..

or how do I save links to doc files in mysql...any ideas??

thanks in advance
Copy linkTweet thisAlerts:
@themartyJun 08.2006 — I tried this: [...] and I get stupid characters[/quote]
Of course, because you're dealing with a word document. You see the same on your screen as you would if you opened a word document with notepad
Copy linkTweet thisAlerts:
@amonamonauthorJun 09.2006 — Yes marty but I am trying to deal only with word doc. so this function file_get_contents in my case is useless...am I wrong?

Thank you for help...
Copy linkTweet thisAlerts:
@themartyJun 09.2006 — so this function file_get_contents in my case is useless...am I wrong?[/quote]

In your openingspost you said that [i]instead[/i] of using the file system you wanted to use a database...So, what i've done is show you how to put a word document in the database.

But i understand now that you are looking for a way to display a word document on the screen in HTML format? I've never worked with it myself, cuz i only use linux servers but the only possibility lies in the [url=http://nl2.php.net/com][b]C[/b]omponent[b]O[/b]bject[b]M[/b]odel[/url]. So have a look at that
Copy linkTweet thisAlerts:
@chazzyJun 09.2006 — if i could butt in, but, storing binary files in a database is considered poor design. it slows down your overall database performance, makes backup/restore a pain. is there anyway you can just store the data on the file system and keep track of the location in the database?
Copy linkTweet thisAlerts:
@themartyJun 09.2006 — if i could butt in, but, storing binary files in a database is considered poor design.[/quote]

Sure, you can butt in, but I can certainly give you some advantages of storing word documents in a database too, even backup advantages - a thing you consider a disadvantage.

I'm not saying it's always good practice, on the contrary, i usually avoid it myself too. But that doesn't mean there are not situations in which it is the better option.

I just finished a content management system for a client in which 5 types of users can login, but only 1 of them is allowed to see certain documents. The security is a major issue here as these documents are very valuable. So, i've opted for storing them in the database. The database itself is already protected of course and the only way to get them out of the database would be to go through my download script that checks the persmissions. I would never have to be scared that when the CMS gets moved to a new server in the future things like .htaccess files are not being put back into place.

Even, we now only have to synchronize the database with another one without having to bother about backing up the filesystem. And i never have to be scared these files are accidentally copied to a place where they don't belong.

So, it's not as black and white as you put it.
Copy linkTweet thisAlerts:
@chazzyJun 09.2006 — I agree with you, there are certain security requirements that make storing documents like that in a database more important. But then why go with PHP if your security is much more important, as you put it?

but as you put it, it's not as black and white in the overall picture, but when it comes to something in php, i would say it's pretty much a no-no. php seems very slow in file handling like this which could create havoc on the file system when pulling from the DBMS (especially mysql).

i too have implemented data storage in oracle over java and oracle w/ a mod pl/sql backend. even on oracle it ran extremely slow. we did get the benefit of version/permission control though, which is what was really desired.

i would say though that if you don't have any security requirements that say only this user group can access it, then you might want to look into other solutions.
Copy linkTweet thisAlerts:
@themartyJun 09.2006 — First of all, it's a nice and interesting discussion, but i'm about to go on holidays, so i won't be able to reply for about 10 days.

I agree with you, there are certain security requirements that make storing documents like that in a database more important. But then why go with PHP if your security is much more important, as you put it?[/quote]

I needed a websolution as these users are all over europe. I have no reason not to use PHP .. certainly not security issues. If you know of any security issues that might be compromising, i'd be very interested to learn about them.

but when it comes to something in php, i would say it's pretty much a no-no. php seems very slow in file handling like this which could create havoc on the file system when pulling from the DBMS (especially mysql).[/quote]

I'm very suprised with your answer

First of all: mysql is well known for it's speed. In fact the main complaint is that mysql focusses too much on speed (and yes, it is very fast) and not enough on stability.

Second: you say php is slow in file handling, but we're not working with files here, but with databases. So i'm not sure why that would be an argument. And even: i will put my php application against your Java application any time :-)

But, again, if you know of any arguments (backed up with some proof of course) why not to use php, i'd be very interested to learn about them
Copy linkTweet thisAlerts:
@chazzyJun 09.2006 — For one, php is compile at run time. A hiccup in the php processor or apache can cause the script to generate its source to the browser instead of its render. Even unlike ASP.NET which is compile one time, the application processing occurs on apache and consumes more resources than expected.

MySQL is not fast, I'm not sure where you got this idea from. Remember the old saying "only believe half of what you read on the internet"? I have actually participated in benchmarks comparing straight sql statements on the same data in both oracle and mysql. I forget the exact query but it generated the cross product tuple of a table (every entry with every other entry aside from itself) and oracle beat mysql (I was quite sure the mysql server was going to shutdown before it gave me the result) in a result of about 10 minutes vs. mysql's 1 hr 49 minutes.
Copy linkTweet thisAlerts:
@amonamonauthorJun 12.2006 — Please...U R off topic..can anyone give me a hint or something...I have PDP mysql apache implemented something like CMS...can U suggest some code for manipulation mysql php and word doc...how can I insert or retreive or display my word doc...please please..any helpr hint is appretieted...

Thanks in advance..
Copy linkTweet thisAlerts:
@chazzyJun 12.2006 — I think you really don't understand what you're trying to do.

[code=php]echo file_get_contents($filename);[/code] should show binary code, it's what it is. you should not be seeing the clear text - you don't seem to get that part. you should be inserting these contents to the database in a blob column, you can't actually read them though. you just need to insert it to the database, then serve them out when they want to get the file.

also, if you really think that our discussion is off topic, you really dont' understand the complexity of what you're trying to do.
Copy linkTweet thisAlerts:
@HuevoosJun 12.2006 — i would say it's pretty much a no-no. php seems very slow in file handling like this which could create havoc on the file system when pulling from the DBMS (especially mysql).
[/QUOTE]

php don't have to deal with the files.

if you use a just files table containing id name file and maybe the id of the group that can access and make your queries as "SELECT ´id´, ´name´ FROM files where ´group´ = 1"; mysql won't slow down much
Copy linkTweet thisAlerts:
@amonamonauthorJun 13.2006 — Ok guys I have implemented successfuly upload of files with this function

$content = file_get_contents ('E:/path2/directory/'.$fileName);

and then

$query = "INSERT INTO upload (name, size, type, content ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";


and I have all that in database but what is effective way of reading all those files??

Is there a way read those files???

not to mention edit them...if someone knows how please help any code is very welcome..


Thanks in advance
Copy linkTweet thisAlerts:
@chazzyJun 13.2006 — this is one of the reasons why i said to not put it in the database.

  • - you cannot edit them. they're word docs, and thus you can't really edit them outside of word.


  • for serving them, look at this topic: http://www.webdeveloper.com/forum/showthread.php?t=109952

    it should give you the framework of what you need to do.
    Copy linkTweet thisAlerts:
    @amonamonauthorJun 14.2006 — ok chazzy...I was also wondering how to read those files...but could not figure how...thank U for your reply...

    Cheers,
    ×

    Success!

    Help @amonamon spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...