PDA

View Full Version : PHP: writing files


jadero
02-03-2002, 06:10 PM
Grab a coffee; this is a long one :)

I have a javascript library (patience, I'll get to the PHP stuff in a moment), which depends on finding its data in arrays. Those arrays are currently a series of stand-alone files (i.e. contains only the array declaration and the assignment of the various elements). Since the data does change over time, I built a MySQL database to handle the data, thus simplifying the data updates. That part is all working fine, but I'm having trouble deciding what to do next.

As I see it, I've got two choices:
1. I can query the relevant table and use PHP to output actual disk files that are identical to my current JS files. This is attractive for a couple of reasons. First, it means that I don't have to make any other changes to my site coding. Second, since the data actually changes quite slowly over time, having 'static' files that are rewritten occassionally strikes me as being an efficient use of memory, processor, and MySQL resources, while little or no effect on HTTP server and I/O resources (other than actual disk space).

2. I can (I think!) modify the 'data loading routine' to actually build the JS array on the fly with 'echo()' statements. This is attractive for a couple of reasons. First, I don't have to worry about trying to figure out how to actually write a file (which looked simple enough, but has proven to be a little more complicated than I expected). Second, if I ever find the need to build advanced search functions instead of depending on a few pre-defined listings, the 'on-the-fly' method will need to be built anyway.

So here's what I need to know:
1. Can I use PHP on FQ to write files derived from MySQL queries to my web-folders? Are there security issues associated with this (either mine or FQs) that should keep me awake at night?
2. Is there really a meaningful performance difference between hitting a static file and pulling a query? I expect that a lot has to do with file/set size, so I'll give you the basics: Maximum 300 rows in a single result set, with a maximum row length of 300 bytes (I have medians and averages as well, but there is also a trend that suggests I should be prepared to have most of the results max'd out!).
3. Is there anything I need to know that I don't know I need to know (if you know what I mean %) ) or anything else you need to know in order to provide sound advice?

Thanks for any help you folks can provide.

Ron

Arthur
02-04-2002, 09:21 AM
1. Yes, you can use PHP to write out the result of a query to a file. BUT, you can do this two ways. The first is you load the script as a webpage. Here you have to watch out with file permissions. The PHP module runs under the same userid as the Apache webserver, so files written by it are also owned by that user id. That means, if you don't chmod it to 646 or similar, you can't delete the files (other than deleting it from within PHP).
The second way would be easier; from the command-line invoke PHP on the script. No problems with file permissions there.
There are no security issues that I can think of. The only thing is that if people know the filename they can start the script themselves, not really an issue and you could also move the script outside the www tree.

2. It depends on how complex the query is. If it's a straight forward select query on a relatively small table, then there's very little difference in performance. It's hard to say where the point is where it really starts to matter.

3. I don't know what you know, you know ;) A tip though; install MySQL, PHP and Apache locally on your computer. That way you can experiment all you want without breaking your site.

Hope that helps.

jadero
02-04-2002, 08:30 PM
I've pretty much decided to go with a pure query solution for now. It's really the most flexible and easiest to build and maintain. The only way scalability will be an issue is if the project is successful. If it's successful, I'll be able to afford stuff like dedicated hosts and programmers that are smarter than I am long before I outgrow FQ. If it's not successful, well at least I'm having fun!

Thanks again.