|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Feb 2010
Posts: 5
|
Why do we create 'files'? "fopen($filename, 'w')
Hello Sitepoint PHP,
Quick question regarding the use of files; creating, reading, writing, etc.. As I understand you can create text files to store information and recall or append them later on. How is this function used in php development? Could you not use MySQL to store the same information? I searched online to see if there were any posts explaining why and how this function is used but turned up nothing but actually tutorials on how to use it. Thank you, Chris |
|
|
|
|
|
#2 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 13,104
|
It all depends on what you intend to do with the files afterwards. If the file you are creating is intended to be downloaded and read into another program then saving the information in a database is not an option as you still need to create the actual file before it can be downloaded.
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
#3 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Are you seriously saying that files have absolutely no use?
What about logs? Do you want to make a DB query every time you need to log (i.e. every page)? Sessions? XML? Queued emails? Configuration? Uploaded files? This can't be a serious question.
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
|
|
#4 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Oct 2008
Location: Melbourne
Posts: 772
|
It's a perfectly reasonable question, I think. Of course you can do stacks of stuff with a database, including storing files. The heart of this question seems to be more about why you might choose to do one or the other.
Let's try and shed some light on the pros and cons of each approach before we dismiss the whole idea out of hand.
__________________
"I'm Commander Shepard, and this is my favourite post on the internet." We'll miss you, Dan Schulz. Last edited by raena; Feb 16, 2010 at 17:20.. Reason: please don't dismiss the newbies |
|
|
|
|
|
#5 |
|
SitePoint Member
Join Date: Feb 2010
Posts: 5
|
Thank you raena, I am new to PHP so initially I was curious as to what tasks would be better suited for file creation rather than using a database.
Perhaps Aliendev could you elaborate a bit on some of your examples? Last edited by gearedtech; Feb 16, 2010 at 17:23.. Reason: correction |
|
|
|
|
|
#6 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Quote:
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
|
|
|
#7 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Actually, here is an example I have on hand. It records who is active on the website and when, to create a "currently online" feature. Doing this in a DB is of course possible but adds unnecessary overhead when a simple file works.
php Code:
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
|
|
#8 |
|
Program Your Site Team
![]() ![]() ![]() Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 11,231
|
To me it's about how often the data is going to change and be needed. For example.
I have an RSS feed that reads from a (relatively small - 12KB) CSV file, the data doesn't change that often so it's easy for me to edit-upload the file when necessary. One of my plugins writes to log files. They may be appended to several times during a day, but after that day they're essentially static. Only the admin may need to read them later. WordPress uses a database for most everything except the config file, yet if someone wants to speed things up, it's recommended to cache (create files for) the post pages. IMHO I don't consider optimized queries to an optimized database to be slow at all, yet there is always the question of a possible database error as opposed to a possible filesystem error. I don't know as one is all that much faster or reliable over the other, but it's something to consider. Probably the real bottleneck is poorly written PHP code.
__________________
10 Rules for Driving Traffic Using Forums Ultimate SEO Checklist How to be a Great Online Community Member Fluff posting - what is it and how do we deal with it? SitePoint's September Contest!! Do You Want to Be Our Judge? |
|
|
|
|
|
#9 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Jan 2008
Posts: 574
|
Quote:
|
|
|
|
|
|
|
#10 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Jan 2008
Posts: 574
|
Quote:
When I built my own framework I designed it so that mod_rewrite passes control to PHP only when the file doesn't exist. The .htdocs folder of the project becomes my main cache area - the framework php files themselves are all stored elsewhere with only one PHP file in the .htdocs folder called pamwf.php which the mod_rewrite code points to (and if I could bypass the creation of said file I would). This way when possible static file sends are used at all times. |
|
|
|
|
|
|
#11 |
|
Program Your Site Team
![]() ![]() ![]() Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 11,231
|
I wasn't talking about HTTP requests for static files vs. HTTP requests for dynamically created files. I meant a dynamically created file pulling data by reading a file vs. querying a database. (well, in the beginning and last part of the reply). As said, to speed WordPress up, ditch the dynamic db pages and create static cached pages (middle of the reply).
Now even I'm getting confuzzled.
__________________
10 Rules for Driving Traffic Using Forums Ultimate SEO Checklist How to be a Great Online Community Member Fluff posting - what is it and how do we deal with it? SitePoint's September Contest!! Do You Want to Be Our Judge? |
|
|
|
|
|
#12 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Jan 2008
Posts: 574
|
Quote:
That's the approach I use with this caveat - I still use PHP snippets to handle file expiry, taking advantage of PHP's ability to write a file it can eval as code later. While this does involve using the parser I can dodge my framework startup (starting my framework involves reading at least 10 files because it is Object oriented) and the overhead associated with that. |
|
|
|
|
|
|
#13 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Location: Alkmaar, The Netherlands
Posts: 674
|
Quote:
![]()
__________________
Turnware MVC - A new barebone, fast and flexible PHP5 framework! Ruben Knol Turnware MVC Lead Developer |
|
|
|
|
|
|
#14 |
|
Floridiot
![]() ![]() ![]() ![]() ![]() Join Date: Mar 2004
Location: Clearwater, FL
Posts: 782
|
The thing that gets me every time I see a file VS database conversation is that people seem to forget that databases store data in files. Technically, a database is nothing more than an application which provides structured access, statistics about, and easy ways to replicate, data that's in files.
You can't really compare database engines in general, to basic file system functions. Technically the database engines are going to be using those same file system functions anyways. You can compare database engines, to other applications designed to store and retrieve data from files though. Where database engines shine is reading. They do so because the engine maintains indexes about where certain types of data are currently kept in files. Whereas basic file system functions don't really track what's in the files as much as they do track the files themselves. These indexes however, put database engines behind basic file system functions by themselves when it comes to writing. While the file system functions can just seek to the file, determine where to start writing, and write, the database engine typically has to update the indexes as well which means looking at the data and determining where to put it along with what indexes need to be altered/recalculated/etc. To get a picture of how this works, imagine you have someone standing in the back of a moving truck taking boxes from you and placing them in the back of the truck. Basic filesystem functions would take the box, find somewhere in the back of the truck the box would fit without wasting a bunch of space, and be ready for another box immediately after putting it in that spot. A database engine on the other hand, would look in the back of the truck, then look in the box, then look a categorized list of things that have already been put in the back of the truck, determine if there are enough of a certain type of item already in the truck to trigger lunch time, update the list of items, recalculate the statistics about everything in the back of the truck, and a few other odds and ends before being ready for another box. This is why you typically don't see things like server access logs using a database. You might see something that takes previous logs and indexes them using a database engine, but not so much something that logs directly to a database engine. Imagine that same moving truck, but now imagine there are a dozen people bringing boxes all at the same time instead of just you.
__________________
Picksel Color Picker |
|
|
|
|
|
#15 |
|
Program Your Site Team
![]() ![]() ![]() Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 11,231
|
Off Topic: @joebert, Another night-owl, or early riser? I like that analogy. Now imagine someone coming and saying "I need the blue dishes back". So I guess the moral is "What are you planning on doing with the data?" which goes back to #2 felgall
__________________
10 Rules for Driving Traffic Using Forums Ultimate SEO Checklist How to be a Great Online Community Member Fluff posting - what is it and how do we deal with it? SitePoint's September Contest!! Do You Want to Be Our Judge? |
|
|
|
|
|
#16 |
|
SitePoint Enthusiast
![]() Join Date: Sep 2004
Location: Bangalore / Patna, India
Posts: 65
|
If you do not want to use file system using fopen etc and also do not want to use Database then SQlite is a good option. It is file base database. So it will take care of many things like database does but using file.
|
|
|
|
|
|
#17 |
|
Program Your Site Team
![]() ![]() ![]() Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 11,231
|
My localhost uses MySQL in mysql/data/[db name]/ are files
db.opt - containing the config info eg. Code:
default-character-set=latin1 default-collation=latin1_general_ci [table name].MYD - binary and text (the "data") [table name].MYI - binary These files are the database/tables
__________________
10 Rules for Driving Traffic Using Forums Ultimate SEO Checklist How to be a Great Online Community Member Fluff posting - what is it and how do we deal with it? SitePoint's September Contest!! Do You Want to Be Our Judge? |
|
|
|
|
|
#18 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Feb 2008
Location: end($world)
Posts: 834
|
Quote:
|
|
|
|
|
|
|
#19 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Jan 2008
Posts: 574
|
But you have to do that anyway so your point is? (and mod_rewrite in the httpd.conf file does not incur a noteworthy performance hit unless you cause after trillionths of a second).
|
|
|
|
|
|
#20 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 13,104
|
Why are you arguing that using a hammer to insert screws is better than using a screwdriver for the task?
You should use the most appropriate tool for each task and where the most appropriate is to create a file you should do that rather than using some convoluted approach to avoid it by creating a file with more complex interface (a database) instead.
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
#21 |
|
SitePoint Member
Join Date: Nov 2009
Location: UK
Posts: 11
|
In my opinion is more safe with database then text files. You should use mysql databases if you need security for your informations and phpmyadmin user to easy change edit or add something there. All the best.
__________________
Satnav Money Online Marketing |
|
|
|
|
|
#22 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Quote:
Null point.
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
|
|
|
#23 |
|
Program Your Site Team
![]() ![]() ![]() Join Date: Jul 2005
Location: West Springfield, Massachusetts
Posts: 11,231
|
@Satnav Good point. In terms of letting users supply the file / db input, security-wise using filesystem functions to only allow certain types/extensions, getting the permission levels right etc. may be more invovled than using a database and the security related functions. There again it boils down to the PHP code used.
@AlienDev Hence the importance of not keeping sensitive files below the root.
__________________
10 Rules for Driving Traffic Using Forums Ultimate SEO Checklist How to be a Great Online Community Member Fluff posting - what is it and how do we deal with it? SitePoint's September Contest!! Do You Want to Be Our Judge? |
|
|
|
|
|
#24 |
|
Programming Since 1978
![]() ![]() ![]() ![]() Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 13,104
|
It still comes down to what you are using the data for. There is no point whatever in storing the information dynamically in a database when the information needs to be downloaded as a file. If you store it in a database then you need a second lot of code to convert the info from the database into the file. That second lot of code would need to run every time someone requests the file whereas if you create the file directly there is no second lot of code needed to run.
Sure storing it in a database and dynamically generating the file can avoid the need to use fopen() but is serves no other purpose other than to make things more complicated than necessary and to slow things down.
__________________
Stephen J Chapman HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog Book Reviews, About JavaScript scripts and tutorials follow me on Twitter |
|
|
|
|
|
#25 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Feb 2007
Location: UK
Posts: 594
|
Right, then where exactly DO you store database details? It has to be somewhere. Did you even think before posting?
__________________
Me on StackOverflow | Blog & personal website. I mostly use: PHP, Java, JavaScript, Android. |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 13:34.











Now even I'm getting confuzzled.




Linear Mode
