0

Well,I am learning the web development and currently working on PHP and mySQL,I am just totally a newbee to database concepts.To be honest Iam not sure when to create a table or when to create a database.I need some suggestions and help from you.

Okay I have these doubt kindly clear me this.I am not much aware of this but how much security does php file concepts provide us. Is there any harm in using file concepts of php?

Okay let me tell you these I want to save some data that user has entered into a text file on the server, the data might be like some message or something like his information,I wanted to save the data in a file and then save its directory path in the database. and while retrieving the data just get the file path from the database then retrieve it from the text file. Is this a good or bad idea of doing it? or should I need to save the user data in the database itself?

Similarly I also want to save the path of the images or pictures in the database and then just put the path in <img>tag.I got no one here to help me with this questions.So please help me with this,Any help is greatly appreciated.

Kindly let me know what is the way I should choose to do ?

niko
  • 9,285
  • 27
  • 84
  • 131
  • I also asked myself that questions when I was making my first website. My answer is - use MySQL. – Roman Jan 16 '12 at 17:44

4 Answers4

2

For images and other file-bound resources, it makes sense to store the image on the file system and the path to it in the database. After all, file systems are great for storing files. You can store the file as a binary field in the database, so that's certainly an option. There are pros and cons either way. Personally, I prefer to keep the files on the file system.

I'm not sure where you're going with this:

the data might be like some message or something like his information,I wanted to save the data in a file and then save its directory path in the database

Is there a particular reason why this data needs to be in a file? Or are you just not sure how to store it in a database? If the data is structured and consistently organized then I can't imagine a reason not to keep it in a relational database. (And even if it isn't as structured, I'd probably still look into a database over the file system for this sort of thing.)

Images and other non-relational resources are generally file-bound, so keep them in files. But if you're just storing text in a text file for no other reason than that's what you've always done, I'd recommend using a database.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • what is the max length of varchar in database ? I chose text file because the entered input by the user is unknown length – niko Jan 16 '12 at 17:51
  • @niko: Well, `longtext` can go up to 2^32 bytes, which is 4GB of text. `varchar` by itself can go up to I believe over 65,000 characters, which is quite a bit for user-input text. More information here: http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html Is it really just free text input? When you say "something like his information" I'm picturing more of a structured user profile or something to that effect. – David Jan 16 '12 at 18:02
1

PHP provides only as much security as the underlying filesystem. But putting files on disk and saving the path in the db is the traditional method.

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
  • So please could you tell me what would be the best way to do it ? – niko Jan 16 '12 at 17:44
  • I think you are on the right track. Make sure the files are not in a path accessible to the web server (to prevent direct linking) and proceed. You could even rename / alias them when you save them. – ethrbunny Jan 16 '12 at 17:56
0

Files in a database are generally not the best solution. But that's mostly because people talk about storing binaries (e.g. images, zip files, etc...) which would be an opaque blob as far as the database is concerned.

With text files, especially small ones, it'd still be at least somewhat useable by the DB, even if only via SUBSTR() type matching/searching, so this is one case where storing in the DB could make sense.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

There is a good rule-of-thumb here:

  • If it is something, the DB "understands" (such as text), store it in the DB - you might later want fulltext indexing, search, text transformation, whatever.
  • If not (e.g. Images), store it in files

as all rules of thumb, this might or might not fit your index finger

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92