Log in

View Full Version : php scripting problem ... again


Frank
08-12-2004, 06:19 PM
I turn to the few that helped me with my PHP before to help me again

This function needs to create a vertical, alphabetically sorted list of all JPG, GIF and PNG files in a directory, and output it to html links.

right now, if you go to http://www.dreamwill.net/images/list.php, it will list it by last updated date, and doesnt run any checks for extension. I
dont want that. I got it from Ray, the owner of themaxx.com, so it was the way HE wanted it.

I'm working offline with a test copy (the code below), but its giving me :

Parse error: parse error, unexpected $end in c:\inetpub\wwwroot\test\list.php on line 99

what am I not seeing here...


<?php


$baseUrl = "http://www.dreamwill.net/";

$baseUrl .= "images/";

// outputs the html links.. this is the part you're probably most interested in.
function outputFileList( $dir ) {

$fileList = array();

$gif = 1;
$jpg = 1;
$png = 1;
$index = 0;

$dir = opendir(".");

// builds an associative array of files indexed by modification time
// so we can sort them accordingly. (i'm not actually 100 percent certain
// that this works quite right, but for my purposes it's close enough. probably
// shouldn't use it for controlling a nuclear device or for someone's heart monitor.
while( ($file = readdir($dir)) ) {

if ($file != "." and $file != "..") {

$myfile = explode (".", $file); // test whether is a gif file
// test extensions!
if($gif) {
if($myfile[1] == 'gif' || $myfile[1] == 'GIF') // if it's a gif file
{
$fileList[$index] = $file; // then add it to the array
$index ++;
}
}
if($jpg) {
if($myfile[1] == 'jpg' || $myfile[1] == 'JPG') // if it's a gif file
{
$fileList[$index] = $file; // then add it to the array
$index ++;
}
}
if($png) {
if($myfile[1] == 'png' || $myfile[1] == 'PNG') // if it's a gif file
{
$fileList[$index] = $file; // then add it to the array
$index ++;
}
}
}

// sort the array by key (modification time)
sort($fileList);

// // css class (alternates on each row)
// $class = "oddfile";

// counter used to limit the number of items output.
$num = 0;

// iterate over the list
while ((list ($key, $val) = each ($fileList)) && $num < 100 ) {

// alternate css class
$class = ($class == "oddfile") ? "evenfile" : "oddfile";

// keep track of how many we've output
$num++;

// get the filesize for the tooltip
$filesize = filesize($path."/".$val);

$filename = urldecode($val);

// output the link.
// i'm doing a bit of string manipulation here to translate the image filename into
// the name of the corresponding php file. you'll want to tweak this to your needs.
echo '<span style="margin-right:10px;" class="' . $class . '">' . $num . '</span>';
?><a href="<?php echo $baseUrl ?><?php echo str_replace( "images", "pages", $path ) ?>/<?php echo $val ?>" title="<?php echo urldecode($val)?> : <?php echo urldecode($filesize)?> bytes" class="<?php echo $class?>"><?php echo substr($val, 0, 30)?></a><br>
<?php
}
}
?>

Senseless
08-13-2004, 07:19 AM
unexpected $end is usually a braces (brackets) problem. ill have a look at it in a couple of minutes.

Senseless
08-13-2004, 07:29 AM
ok well got it sorta maybe working. firstly, you're missing a closing brace at the very end to close the entire function loop, so just add that in.

second, i'm not sure if this is the way you want it, but you need to call this function from another file (i think).

so make another file, and include this one and call the function. i.e.

<?php include("list.php") ?>
<?php outputFileList();
?>

if this all makes no sense plz excuse me i've been playing doom3 for 16 straight hours

Frank
08-13-2004, 03:25 PM
here's what I have now : http://www.dreamwill.net/test/

edit :
<s>if you look at the numbers, it lists the files alphabetically now, but something seems to be wrong with my loop.

it works fine now :D

Steve Strong helped me create a different way of populating the array.

now, all thats left is setting up the output a href to not open in a blank screen, but rather do the same action as the numbered links, which is recreate index.php with the layout and the image.</s>

All thats left now is placing the php that will generate the IMG tag in the right place.


<?
$the_array = Array();
$handle = opendir('.');
$counter = 1;

$current_url =$_SERVER['PHP_SELF'];

while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (preg_match("/gif/i", $file) || preg_match("/jpg/i", $file) || preg_match("/png/i", $file)) {
// if the file has the proper image extension...
$the_array[] = $file;
} else {
//do nothing... don't add it.
}
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
while (list ($key, $val) = each ($the_array)) {
print ("$counter - <a href='$current_url?num=$val'>$val</a><br>");
$counter ++;
}

$totalfiles = count($the_array);

print ("<P>files in Directory : ". $totalfiles);

?>

Senseless
08-16-2004, 07:56 AM
ok is this working or not what needs to be done? sorry soryr very tiedred

Frank
08-16-2004, 11:57 PM
its somewhat working now. I got a guy I know to help me out while offline. still trying to get thought another parse error but it shouldnt be a problem


<?
$the_array = Array();
$handle = opendir('.');
$counter = 1;

$current_url =$_SERVER['PHP_SELF'];

while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (preg_match("/gif/i", $file) || preg_match("/jpg/i", $file) || preg_match("/png/i", $file)) {
// if the file has the proper image extension...
$the_array[] = $file;
} else {
//do nothing... don't add it.
}
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
while (list ($key, $val) = each ($the_array)) {
print ("$counter - <a href='$current_url?num=$key'>$val</a><br>");
$counter ++;
}

if (isset($_GET['num'])) {
print ("</td>\n<td><center><img src=\"". $the_array[$_GET['num']] .."\"></center>\n");
}


$totalfiles = count($the_array);

print ("<P>files in Directory : ". $totalfiles);

?>

Senseless
08-17-2004, 07:12 AM
you've got a useless append dot on line 27.

this:


if (isset($_GET['num'])) {
print ("</td>\n<td><center><img src=\"". $the_array[$_GET['num']] .."\"></center>\n");
}


should be


if (isset($_GET['num'])) {
print ("</td>\n<td><center><img src=\"". $the_array[$_GET['num']]."\"></center>\n");
}


toit. php is fun fun happy time

EDIT: oh and i assume you want it to list vertically, rather than horizontally. then you need to find


while (list ($key, $val) = each ($the_array)) {
print ("$counter - <a href='$current_url?num=$key'>$val</a>");
$counter ++;
}


and change it to


while (list ($key, $val) = each ($the_array)) {
print ("$counter - <a href='$current_url?num=$key'>$val</a>**");
$counter ++;
}


EDIT 2: ok for some reason it won't display my changed code so just put a an html break where the ** is.

i have a sneaky suspicion that you have already done this, and the forum has eaten your breaks, just like it did mine.

evil, evil forum.

Frank
08-17-2004, 03:21 PM
well no matter, it works now : http://www.dreamwill.net/images

:D

Senseless
08-18-2004, 07:58 AM
thats great.

dammit frank i feel this thread really brought us together man