Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Feb 16, 2010, 16:51   #1
gearedtech
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
gearedtech is offline   Reply With Quote
Old Feb 16, 2010, 16:54   #2
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
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.
felgall is online now   Reply With Quote
Old Feb 16, 2010, 16:55   #3
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
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.
AlienDev is offline   Reply With Quote
Old Feb 16, 2010, 17:19   #4
raena
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
raena is offline   Reply With Quote
Old Feb 16, 2010, 17:21   #5
gearedtech
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
gearedtech is offline   Reply With Quote
Old Feb 16, 2010, 18:28   #6
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
Quote:
Originally Posted by gearedtech View Post
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?
Sure.
  • Logs - you virtually always want to log at least 1 piece of info about every page hit. That could be IP, referer, etc. Database queries are slow, so adding 1 query to every page load will slow your website significantly with high traffic volumes. Also, these are literally never searched in the way tables are.
  • Sessions -Same as above. The data they hold is undefined and may be big and multidimensional, so there is no point trying to put it in a DB.
  • Configuration - This will typically contain (among other things) the database connection details, so those obviously have to be in a file (XML, YML, whatever). Also, these files will grow to have many different keys and structures.
  • Uploaded files -the are some people who think storing images in a database is a good idea, but they are idiots, so ignore them.
Basically, anything that you don't NEED to do joins/searchs on, or that isn't an exactly defined data set, shouldn't be stored in the database without good reason.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 16, 2010, 18:31   #7
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
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:
class LatestOnlineTable extends Singleton
{
    private $file, $list;
 
 
    protected function __construct()
    {
        $this->file = '/data/latest-online';
        $data = (is_file($this->file))
            ? file_get_contents($this->file)
            : false;
 
        $this->list = ($data !== false)
            ? unserialize($data)
            : array();
 
        $this->purgeOld(600);
    }
 
 
    /**
     * Remove entires that are now past the expiry limit.
     *
     * @param integer $secondLimit
     */
    private function purgeOld($secondLimit)
    {
        $now = time();
 
        foreach ($this->list as $username => $time)
        {
            // Remove entry if old
            if ($time < ($now - $secondLimit))
            {
                unset($this->list[$username]);
            }
        }
    }
 
 
    /**
     * Set a username's timestamp to right now.
     *
     * @param string $username
     */
    public function touch($username)
    {
        $this->list[$username] = time();
    }
 
 
    /**
     * Get a list of usernames that were recently active.
     *
     * @return array
     */
    public function getActiveMembers()
    {
        return array_keys($this->list);
    }
 
 
    public function __destruct()
    {
        file_put_contents($this->file, serialize($this->list));
    }
}
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 16, 2010, 18:57   #8
Mittineague
Program Your Site Team
silver trophybronze trophy
 
Mittineague's Avatar
 
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.
Mittineague is offline   Reply With Quote
Old Feb 16, 2010, 19:16   #9
Michael Morris
SitePoint Evangelist
 
Michael Morris's Avatar
 
Join Date: Jan 2008
Posts: 574
Quote:
Originally Posted by felgall View Post
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.
Not true. Using mod_rewrite and the proper header calls to get the mime type right PHP can, in theory, output everything from the database without ever creating a file. It's going to be slower than outputting files at all times though bringing up the more pertinent question of *should* it be done. Because it certainly *can* be done.
Michael Morris is offline   Reply With Quote
Old Feb 16, 2010, 19:23   #10
Michael Morris
SitePoint Evangelist
 
Michael Morris's Avatar
 
Join Date: Jan 2008
Posts: 574
Quote:
Originally Posted by Mittineague View Post
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.
Static is always faster than dynamic. Doesn't matter the language, and no amount of skill on the programmer's part is going to come within spitting distance of closing the gap.

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.
Michael Morris is offline   Reply With Quote
Old Feb 16, 2010, 19:38   #11
Mittineague
Program Your Site Team
silver trophybronze trophy
 
Mittineague's Avatar
 
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.
Mittineague is offline   Reply With Quote
Old Feb 16, 2010, 19:53   #12
Michael Morris
SitePoint Evangelist
 
Michael Morris's Avatar
 
Join Date: Jan 2008
Posts: 574
Quote:
Originally Posted by Mittineague View Post
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.
That's what I thought you meant and you're right - doing a file passthrough is not likely to be any faster or slower than using a database unless the file is the end result of a LOT of queries (say, more than 10). But if you're able to build a cache file why not put it where Apache can statically read it and dodge PHP entirely?

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.
Michael Morris is offline   Reply With Quote
Old Feb 16, 2010, 23:38   #13
Ruben K.
SitePoint Guru
 
Ruben K.'s Avatar
 
Join Date: Jun 2005
Location: Alkmaar, The Netherlands
Posts: 674
Quote:
Originally Posted by Michael Morris View Post
Static is always faster than dynamic. Doesn't matter the language, and no amount of skill on the programmer's part is going to come within spitting distance of closing the gap.

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.
Do keep in mind that checking if the file exists with mod_rewrite adds an overhead performing that action
__________________
Turnware MVC - A new barebone, fast and flexible PHP5 framework!

Ruben Knol
Turnware MVC Lead Developer
Ruben K. is offline   Reply With Quote
Old Feb 17, 2010, 00:50   #14
joebert
Floridiot
 
joebert's Avatar
 
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
joebert is online now   Reply With Quote
Old Feb 17, 2010, 01:34   #15
Mittineague
Program Your Site Team
silver trophybronze trophy
 
Mittineague's Avatar
 
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
Mittineague is offline   Reply With Quote
Old Feb 17, 2010, 01:37   #16
satya prakash
SitePoint Enthusiast
 
satya prakash's Avatar
 
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.
satya prakash is offline   Reply With Quote
Old Feb 17, 2010, 01:56   #17
Mittineague
Program Your Site Team
silver trophybronze trophy
 
Mittineague's Avatar
 
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].frm - binary and text
[table name].MYD - binary and text (the "data")
[table name].MYI - binary

These files are the database/tables
Mittineague is offline   Reply With Quote
Old Feb 17, 2010, 05:01   #18
risoknop
SitePoint Guru
 
risoknop's Avatar
 
Join Date: Feb 2008
Location: end($world)
Posts: 834
Quote:
Uploaded files -the are some people who think storing images in a database is a good idea, but they are idiots, so ignore them.
I would actually argue that it is a good idea to store some images in the database. Personally, I think avatars are a good candidate for an image that would do well in the database. Of course, photos, wallpapers and other images should be on filesystem.
risoknop is offline   Reply With Quote
Old Feb 17, 2010, 10:28   #19
Michael Morris
SitePoint Evangelist
 
Michael Morris's Avatar
 
Join Date: Jan 2008
Posts: 574
Quote:
Originally Posted by Ruben K. View Post
Do keep in mind that checking if the file exists with mod_rewrite adds an overhead performing that action
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).
Michael Morris is offline   Reply With Quote
Old Feb 17, 2010, 10:51   #20
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, NSW, Australia
Posts: 13,104
Quote:
Originally Posted by Michael Morris View Post
But you have to do that anyway so your point is?
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.
felgall is online now   Reply With Quote
Old Feb 17, 2010, 12:05   #21
Satnav
SitePoint Member
 
Satnav's Avatar
 
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
Satnav is offline   Reply With Quote
Old Feb 17, 2010, 12:17   #22
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
Quote:
Originally Posted by Satnav View Post
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.
If a hacker can access a below-public file on your server, he can access the file with your DB connection details.

Null point.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 17, 2010, 12:22   #23
Mittineague
Program Your Site Team
silver trophybronze trophy
 
Mittineague's Avatar
 
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.
Mittineague is offline   Reply With Quote
Old Feb 17, 2010, 12:31   #24
felgall
Programming Since 1978
silver trophybronze trophy
SitePoint Award Recipient
 
felgall's Avatar
 
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.
felgall is online now   Reply With Quote
Old Feb 17, 2010, 12:53   #25
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
Quote:
Originally Posted by Mittineague View Post
@AlienDev Hence the importance of not keeping sensitive files below the root.
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.
AlienDev is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 13:34.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved