View Full Version : Reading File

03-24-2007, 10:36 PM
Ok here's what I need to do. I need to read a file that looks like this:

g1.jpg::Description #1
g2.jpg::Description #2

I need each side of that "::" to be a different variable and I need it in some kind of while loop till the end of the file. I've tried a coupld of split and explode versions and can't get it going, basically because of that end of the line when it starts a new one. This file displays descriptions for pictures in another script, and it splits it by of course as you can see the file name then the "::". But since I'm not reading the file list I can't get that other splitter. I don't want to read the directory list because in case the file isn't there anymore I don't want that to conflict with reading the file out and displaying the contents. Basically what I want to do is have an input box for each one, and then at the end of the php file allow the user to click on save to save it back to the file if they edited anything.

03-25-2007, 01:23 AM
Explode by /n first then by ::

03-25-2007, 01:42 AM
1. spool the file into and array using file (http://www.php.net/file)
2. loop thru the array using foreach (http://www.php.net/foreach)
3. on each iteration run each value thru trim (http://www.php.net/trim)
4. explode (http://www.php.net/explode) each value by "::"

03-25-2007, 08:05 PM
Ok I got that working just fine, thanks for the help. Now I got another question for you. I have all of this put into an array that displays it all in their seperate little input box's for changing. I know how to add a new one to the end of the file, I just append it, not big problem. But how do I delete one of the lines entirely?? This is my script as it stands now:

$lines = file('description.txt');
$i = 0;

foreach ($lines as $line) {
$trimmed = trim($line);
$wordChunks = explode("::", $trimmed);
print"Name of File: <input name=\"part1_$i\" type=\"text\" value=\"$wordChunks[0]\"><br>";
print"Description for File: <input name=\"part1_$i\" type=\"text\" value=\"$wordChunks[1]\"><br><br>";


03-25-2007, 08:10 PM
Post up how you are putting the file back together after editing it.

03-25-2007, 08:39 PM
Haven't gotten that far yet in the scripts. Should I of??

03-25-2007, 09:27 PM
Well you said you can edit and are looking for a way to delete... You can use a checkbox to mark as delete when you update the edits. And then when you build your file back out with the changes, you can remove the ones you want deleted.

03-25-2007, 11:17 PM
Ok thanks for the idea of the checkbox, works great. Now I've got a loop going and then the write info for the file. That all works fine, but I can't get a carriage return(new line) at the end of everyline, here's what I got:

$i2 = 1;
while ($i2 <= $_POST['parts']) {
$totalstring = $totalstring.$_POST["part1_$i2"]."::".$_POST["part2_$i2"]."\n";

And this is what it's giving me:

g1.jpg::Description #1 g2.jpg::Description #2

Single line.

03-25-2007, 11:32 PM

That should work for u

03-26-2007, 12:12 AM
Awsome works perfectly now thanks for all the help.

03-26-2007, 07:23 AM
was wondering if there was room for flexibility in the entry of the data to the text file instead of appending new data to the end of the file, could you add it in order for example within the file


g1.jpg::Description #1
g2.jpg::Description #2
g4.jpg::Description #4

g3.jpg::Description #3

so that they are in order?