Welcome to WebmasterWorld Guest from 54.85.162.213

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Edit flat file via php

Remove a line and other lines associated w/ it

     
4:54 pm on Sep 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 10, 2008
posts: 5
votes: 0


Have a web front end to access a flat text file, seperator is \r\n. The file looks like this:

blank line
#Branch Number ###
#Branch City, Branch State
#Description
#User Name
IP
blank line

Basically, postfix reads from this list to check if the device attempting SMTP is "allowed". If so, mail is sent, otherwise rejected.

Users can populate this file thru the web front end, however I've not allowed them to delete anything, that need has now become required.

I want to have the user enter the IP they want to delete and then associate the 4 rows above the IP, as well as the blank line, with the deletion.

New to PHP so this is a bit of a leap for me.

4:57 pm on Sept 10, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 10, 2008
posts:5
votes: 0


Well, I wanted to edit this and qualify a few things. I've got the logic in place to open the file and that works. Getting the data deleted that I don't want is the problem.

Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.

9:14 pm on Sept 10, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.

Yep, that's as good a way as any to do it. You can explode the data into an array using "\r\n\r\n" so it'll be split on the blank lines.

$data = file_get_contents('file.txt');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formated the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
}

// Rejoin the data into one string

// Write the file

3:41 pm on Sept 11, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 10, 2008
posts:5
votes: 0


MattAU, thanks for that. I'll give that shot.
9:32 pm on Sept 24, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 10, 2008
posts: 5
votes: 0


MattAU,

This isnt working for me, this is just a mock up of what I hope to have and the formatting isnt clean but I think you'll get the idea.

The allowed_relay_ip is layed out as follows (actual snippet)

#558 somewhere
#printer
#wes
#09/10/2008, 02:31 PM
45.45.45.1

#413 Columbia, MO
#HP M4345
#Shaw
#09/12/2008, 01:24 PM
172.23.183.137

The actual code at present, I'm experimenting a bit, is below:

<?php
$ip_address = '172.23.42.44';
$data = file_get_contents('/etc/postfix/allowed_relay_ip');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formatted the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
else
{
print $value;
print $key;

}
}
?>

No matter what I set the IP address to it never finds it in the array. For instance, I set the IP address in the above code and have grep the file and indeed it exists. Yet it never shows up when I browse to this page.

The print statements in the else statement are my attempts at getting something to show up.

Not familiar w/ strpos and my PHP in a Nutshell hasn't helped me on this one. Certain this is just an oversight on my part.

I'd appreciate you chiming in w/ some ideas.

Thanks

12:04 am on Sept 25, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


That code should work awreneau.

I changed

$ip_address = '172.23.42.44';

to

$ip_address = '172.23.183.137';

And it found the IP. Of course it can only find something that's actually there... :)

7:01 am on Sept 25, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Also, you can use file [uk.php.net] to read the contents of the file into an array. No need to read and explode. Each to his own though.

dc

3:25 pm on Sept 25, 2008 (gmt 0)

New User

10+ Year Member

joined:Sept 10, 2008
posts: 5
votes: 0


MattAU,

This is the frustrating part of coding. I have grepped the file and the CLI and it displays the IP, running the script using the same IP, returns nothing.

I'll try using the file option instead of file_get_contents.

What did you use to verify that the IP was found, the same print statements as I used?

11:31 am on Oct 22, 2008 (gmt 0)

New User

10+ Year Member

joined:Oct 2, 2008
posts: 8
votes: 0


Hi, I am hoping I can use the above examples to solve my problem? I have a .txt file with blocks of text separated by blank lines. I need to save each block into separate .txt files on the server.
I am a complete novice and so far only managed to load the .txt into an array and echo it back. Any help would be appreciated thanks.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members