Page 1 of 1

PHP Code Help Please

Posted: Tue Dec 23, 2003 9:20 pm
by maxheadroom
Hello -
I'm trying to open a flat text data file. In it I have a list of image files names & words (example test1.jpg, test, test2.jpg, ect). I want to detect which is which and then have a action happen. This is what I have so far -

<?php

$TheFile = "data3.txt";
$Open = fopen ($TheFile, "r");
if ($Open) {
print ("URLs currently listed in the data file:<P>\n");
$Data = file ($TheFile);
for ($n = 0; $n < count($Data); $n++) {
$GetLine = explode("\t", $Data[$n]);
print ("$GetLine[0]<BR>\n$GetLine[1]<P>\n");
}
fclose ($Open);
print ("<HR><P>\n");
}

$filename = "$GetLine";
$extention = GetFileExtention($filename);
echo $extention;
//
function GetFileExtention($filename) {
$ext = explode(".", $filename);
$extention = "";
for($i=1;$i<=sizeof($ext)-1;$i++) {
if($ext) {
$extention .= ".".$ext[$i];
}
}
return $extention;
}

?>

Posted: Tue Dec 23, 2003 11:19 pm
by Gen-ik
How is your text file structured.. ie do you have one image per line, are they seperated with a comma?

If you post an example of how the text file looks I'll be able to help you out :)

Posted: Wed Dec 24, 2003 7:07 am
by maxheadroom
Hello -
here is a copy of the text file -
Button1.gif,doll.jpg,Rat3.jpg,Rat4,Rat5.jpg,Rat6.jpg,Rat7.jpg,Rat8.jpg
as you can see it's all on one line and I have used commas to seprate it

Posted: Wed Dec 24, 2003 9:18 am
by Gen-ik
Ok, in that case the following may work...

Code: Select all

<?php

$file = "theFile.txt";

ob_start();
require($file);
$contents = trim(ob_get_contents());
ob_end_clean();

$images = explode(",", $contents);

foreach($images as $key => $value)
{
    $bits = explode(".", $value);
    $imageList[$key]->name = $value;
    $imageList[$key]->extension = $bits(count($bits)-1);
}

//  This next bit simply displays the contents on screen.
foreach($imageList as $object)
{
    echo "Filename: ".$object->name."<br />";
    echo "Extension: ".$object->extension."<hr />";
}

?>
$imageList will end up being an array full of the images.
To get the name and extension of any of the images you simply do something like... echo $imageList[3]->extension

Hope that helps.