View Full Version : Unique file() function -- PHP

01-17-2007, 06:12 PM
Well, all the credits from this piece of snippet goes to Twey(it's his function with some minor changes).

function newfile($filename, $sep){
$lines = file($filename);
$config = array();
for($i = 0; $i < count($lines); ++$i)
$config[substr($lines[$i], 0, strpos($lines[$i], $sep))] = substr($lines[$i], strpos($lines[$i], $sep) + 1, -1); // -2 for a Windows host
return $config;


This is an extremely useful function in PHP file handling(it makes things a heck lot easier). Hope you find it useful. Kudos to Twey.

01-17-2007, 06:54 PM
Erm... what does it do? Posting a description with either comments or some sort of example, etc. would be helpful for people who might find this script useful.

01-17-2007, 07:32 PM
drj33: It reads the file like this(in this case the newfile.txt)

Array("test1" => "test2", "test3" => "test4", "test5" => "test6");

It is so extremely useful in creating authentication systems. I am sure there is a version I developed somewhere around. I have also created a more complex version.

Sorry for the lack of any examples or anything. :(

01-17-2007, 07:39 PM
Could you show the original txt file, and by what parameters the function is called?

01-17-2007, 07:39 PM
Nevermind, I posted when you posted, answering my question :)

01-17-2007, 08:06 PM
It should be noted that this was created for a special situation (what exactly, I can't remember now), and in most cases parse_ini_file (http://www.php.net/parse_ini_file)() is both easier and more flexible.

I'm not sure this thread is in the right forum.

01-18-2007, 10:00 AM
Twey: How come, it's not in the right forum?

Everyone who sees this post: If you want to know how to use it of why to use it, let me show you a demonstration:

Suppose, you're creating a login system and you need a flat-file text database in the registration you simply append the data to the file users.txt, something like this:

<?php exit();?>
//userid generated from uniqid() or some other techniques

the userdata.txt file:

<?php exit();?>

You've the following code to read the user data/login the user:

$userlist = newfile("users.txt", ":");
$userdata = newfile("userdata.txt", ":");
list($id, $password) = explode(",",$userlist[$_POST["username"]);
if(isset($userlist[$_POST["username"]]) && $password == $_POST["password"]){
echo "Hello ".$_POST["username"]."<br>";
echo "Your email is $userdata[$id]";
$_SESSION["user"] = $id;

In this way you can use only the id to find out anything about the user(something similar to relational databases) but it also gives you more power to update fields, I'll write about updating fields later(I am in school now and I even live coded the above scripts so they might not work, I will post a working version as soon as I get home(hopefully)).

01-18-2007, 10:07 AM
Twey: How come, it's not in the right forum?Well, it's just a function, not a tip or tutorial particularly.

01-25-2007, 09:53 AM
Twey: So, how is a tip supposed to be? I am sorry but I am a newbie in this forum.

01-25-2007, 12:31 PM
A method as to how to accomplish something, usually -- such as "how to use opacity in scripts cross-browser" or something along those lines.

01-25-2007, 03:18 PM
How about it now? I will be posting the whole system soon.

01-25-2007, 10:40 PM
I think the main point is that anything in here should be helpful to nearly everyone.
General things that there are a lot of questions on, such as opacity in all browsers, why you can't protect source code, how to make flash not be the top layer, how to make an email form... etc.
This script is fine... but... doesn't seem so widely applicable.

I think that is basically what Twey is saying.

I still barely get what it does, so, if I don't understand it, and I'm a regular here and I know a good bit about PHP, then I don't think it will be helpful to that many people (though it might be VERY helpful to a few, but just not to many).