View Full Version : PHP:"working with files&directories" / edit record/entry in a txt file , how ?
02-28-2008, 05:42 PM
I have a simple problem that if I solve I will can solve many others, if is easy answer[no databases involved, just "working with files&directories"):
I make a telephone directory file [telephones.txt] with fields like phone, first name, last name, city, state, zip well I add files for add entry, retrieve entry just left edit entry ... I can retrieve an entry, but how save an edited entry in the txt file ?
how in php I redirect user to a specific page eg. retrieve.php ?
php's include("file.html"); ?
02-29-2008, 02:18 AM
Flat files are a pain, that why databases are so much handier. My suggested options would be:
XML. Overhead involved, but you can never go wrong with XML
Simulate Random Access Files by creating explicit data sizes to each of your fields. This will allow you to scan, move, insert, and delete specific blocks nearly as easy as XML allows.
When you edit values, extract a record from the file and append it at the end.
Use an INI style file:
This is meant for configuration files however, and will allow easy read but writing will be complicated.
My personal recommendation would be to use the DOM and XML files if available on your server.
Redirection can be done in html via the:
<meta http-equiv="refresh" content="0;http://newsite.com/page" />
and in php:
As for file inclusion, using a server side language is all I can think of (php, perl/SSI, etc), otherwise AJAX is kinda like what you are asking for but that is for retrieving content data in general.
Hope that helps!
02-29-2008, 02:20 PM
"When you edit values, extract a record from the file and append it at the end."
when I extract a record is deleted ? how to delete and extract toend ?
03-01-2008, 04:42 AM
How you extract is up to the programmer; a delimiter of some sorts is needed. In other words, you need to be able to determine where the location of specified data is.
Another option I didn't mention is to use an Object for your data record, and you can serialize the data you need and store that per line in a file. This allows you to extract each record, load it into the object (easy in php with __sleep and __wakeup commands in PHP 5), and store this data in a structure of some sorts (such as an array). When you are done manipulation (updates, inserts and deletes), serialize each object back into the file.
This is IMO the easiest approach if you have Object Oriented experience. If you are a little more inclined and experienced, I'd brute up a RAF technique which isn't too bad in php - but you need to explicitly specify your data lengths for it to work, and could result in wasted space. I won't go into how this is done though, if you are interested search up on 'Random Access Files' for the concept of how that is done.
In any case, to be quite honest, files really have become a more advanced PHP technique. In the past, they were the primary storage for information, but with the increased popularity of databases they have become somewhat obsolete in many programmers eyes. Personally I don't think this is the case, but the downside is that I find a lot of new programmers paying little attention to how the file handling is done in php - especially in unix based systems.
If you give me all the fields you need tracked (first name, last name, number etc), I can show you how to implement this in an object oriented approach and use a file system with your choice of a datastructure to use.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.