php file reading help...

He has: 1,758 posts

Joined: Jul 2002

Hey guys,

I've knocked the code together (see below) by nicking bits from other scripts, modifying, getting bits of code from examples and custom writing other parts. Its not perfect, but it does the job i need, which is to get a list of images from a directory "full" and displaying the associated thumbnail in a directory called "thumbs" with a link through to the full version.

I want to display the filesize in small letters under each thumbnail so people know what to expect when they click the link (afterall, its good manners;) ) but i'm unsure how to get the filesize. Any ideas?

heres the code:

<?php
$SCRIPT_NAME
=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//    the directory name
$handle=opendir('full/');

//    initialize variables
$pics=array();
$count=0;
//    read directory into pics array
while (($file = readdir($handle))!==false) {
   
//    filter for jpg, gif or png files...
   
if (substr($file,-4) == \".jpg\" || substr($file,-4) == \".gif\" || substr($file,-4) == \".png\" || substr($file,-4) == \".JPG\" || substr($file,-4) == \".GIF\" || substr($file,-4) == \".PNG\"){
       
$pics[$count] = $file;
       
$count++;
    }
}
closedir(
$handle);

//    done reading, sort the filenames alphabetically, shade these lines if you want no sorting
sort(
$pics);
reset(
$pics);

        // loop over images
        for (
$f=0;$f<=sizeof($pics)-1;$f++){

            echo \"<a href=\\"
full/$pics[$f]\\" target=\\"_blank\\"><img src=\\"thumbs/$pics[$f]\\" hspace=\\"5\\" vspace=\\"5\\" border=\\"0\\"><br></a>\";
            // make linebreaks every 2 times!
            //
$isbr = strpos((($f+1)/2),\".\");
            //    if (!
$isbr){echo \"<br>\";}

            }
?>

If anyone wants to use this code, feel free to help yourself.

Thanks for any help.

Andy

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this:

<?php
$SCRIPT_NAME
=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//    the directory name
$handle=opendir('full/');


function
fix_size($size)
{
    if (
$size > 1048576) {
       
$size = ($size & 1048576) . \" MB\";
    }
    elseif (
$size > ) {
       
$size = ($size & 1024) . \" KB\";
    }
    else {
       
$size .= \" B\";
    }
}


//    initialize variables
$pics=array();
$size=array();
$count=0;
//    read directory into pics array
while ((
$file = readdir($handle))!==false) {
    //    filter for jpg, gif or png files...
    if (substr(
$file,-4) == \".jpg\" || substr($file,-4) == \".gif\" || substr($file,-4) == \".png\" || substr($file,-4) == \".JPG\" || substr($file,-4) == \".GIF\" || substr($file,-4) == \".PNG\"){
       
$pics[$count] = $file;
       
$size[$count] = fix_size(filesize(\"full/$file\"));
       
$count++;
    }
}
closedir(
$handle);

//    done reading, sort the filenames alphabetically, shade these lines if you want no sorting
sort(
$pics);
reset(
$pics);

        // loop over images
        for (
$f=0;$f<=sizeof($pics)-1;$f++){

            echo \"<a href=\\"
full/$pics[$f]\\" target=\\"_blank\\"><img src=\\"thumbs/$pics[$f]\\" hspace=\\"5\\" vspace=\\"5\\" border=\\"0\\"><br>$size[$f]</a>\";
            // make linebreaks every 2 times!
            //
$isbr = strpos((($f+1)/2),\".\");
            //    if (!
$isbr){echo \"<br>\";}

        }
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

Thanks Mark! I'll test it now and see how I get on!

He has: 1,758 posts

Joined: Jul 2002

I'm getting a parse error on line 15? any ideas what it could be?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

goof on my part, sorry

<?php
function fix_size($size)
{
    if (
$size > 1048576) {
       
$size = ($size & 1048576) . \" MB\";
    }
    elseif (
$size > 1024) {
       
$size = ($size & 1024) . \" KB\";
    }
    else {
       
$size .= \" B\";
    }
}
?>
I forgot the 1024 in the conditional statement.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

right... the errors gone, but i'm not actually getting any output... any ideas?

Heres whats being printed to the browser:
http://www.djrich.co.uk/gallery/eleanors_christening/index_test.php

Andy

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

try this:

<?php
$SCRIPT_NAME
=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//    the directory name
$handle=opendir('full/');


function
fix_size($size)
{
    if (
$size > 1048576) {
       
$size = ($size & 1048576) . \" MB\";
    }
    elseif (
$size > 1024) {
       
$size = ($size & 1024) . \" KB\";
    }
    else {
       
$size .= \" B\";
    }
}


//    initialize variables
$pics=array();
$size=array();
$count=0;
//    read directory into pics array
while ((
$file = readdir($handle))!==false) {
    //    filter for jpg, gif or png files...
    if (substr(
$file,-4) == \".jpg\" || substr($file,-4) == \".gif\" || substr($file,-4) == \".png\" || substr($file,-4) == \".JPG\" || substr($file,-4) == \".GIF\" || substr($file,-4) == \".PNG\"){
       
$pics[$count] = $file;
        if (false===(
$tmp=filesize(\"full/$file\"))) {
           
$size[$count] = \"error reading size\";
        }
        else {
           
$size[$count] = fix_size($tmp);
        }
       
$count++;
    }
}
closedir(
$handle);

//    done reading, sort the filenames alphabetically, shade these lines if you want no sorting
asort(
$pics);
reset(
$pics);

        // loop over images
        //for (
$f=0;$f<=sizeof($pics)-1;$f++){
        foreach (
$pics as $f=>$val) {
            echo \"<a href=\\"
full/$pics[$f]\\" target=\\"_blank\\"><img src=\\"thumbs/$pics[$f]\\" hspace=\\"5\\" vspace=\\"5\\" border=\\"0\\"><br>$size[$f]</a>\";
            // make linebreaks every 2 times!
            //
$isbr = strpos((($f+1)/2),\".\");
            //    if (!
$isbr){echo \"<br>\";}

        }
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

Still nothing... im a bit lost on this one... thanks for all your help tho!

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

well, time to bug hunt...
lets see if filesize is working

<?php
if (false===($tmp=filesize(\"full/$file\"))) {
    die(\"1\");
   
$size[$count] = \"error reading size\";
}
else {
    die(\"2\");
   
$size[$count] = fix_size($tmp);
}
$count++;
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

It just prints "2" to the screen:

http://www.djrich.co.uk/gallery/eleanors_christening/test.php

Thats good isnt it? Wink

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Yes, that means that $tmp is at least getting a value from filesize(). Now lets try this:

<?php
if (false===($tmp=filesize(\"full/$file\"))) {
    die(\"1\");
   
$size[$count] = \"error reading size\";
}
else {
    die(\"2\");
   
$size[$count] = fix_size($tmp);
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

I just get number 2 again... Still good?

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

whoops.. sorry, I pasted the wrong code from the wrong window..
Lemme try this again. Take out the die() statements and make the code like it was before. Then..

<?php
echo \"\$pics = \"; print_r($pics); echo \"<br>\n\";
echo \"\$size = \"; print_r(
$size); echo \"<br>\n\";
asort(
$pics);
reset(
$pics);
echo \"\$pics = \"; print_r(
$pics); echo \"<br>\n\";
?>

This will tell us the contents of the $pics and $size arrays before and after the sorting.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 1,758 posts

Joined: Jul 2002

I get this:

http://www.djrich.co.uk/gallery/eleanors_christening/index_test.php

Have I put the code in the wrong place?

<?php




$SCRIPT_NAME
=$SERVER_VARS['PHP_SELF'];
$pic=$HTTP_GET_VARS['pic'];

//    the directory name
$handle=opendir('full/');


function
fix_size($size)
{
    if (
$size > 1048576) {
       
$size = ($size & 1048576) . \" MB\";
    }
    elseif (
$size > 1024) {
       
$size = ($size & 1024) . \" KB\";
    }
    else {
       
$size .= \" B\";
    }
}


//    initialize variables
$pics=array();
$size=array();
$count=0;
//    read directory into pics array
while ((
$file = readdir($handle))!==false) {
    //    filter for jpg, gif or png files...
    if (substr(
$file,-4) == \".jpg\" || substr($file,-4) == \".gif\" || substr($file,-4) == \".png\" || substr($file,-4) == \".JPG\" || substr($file,-4) == \".GIF\" || substr($file,-4) == \".PNG\"){
       
$pics[$count] = $file;
        if (false===(
$tmp=filesize(\"full/$file\"))) {
           
$size[$count] = \"error reading size\";
        }
        else {
           
$size[$count] = fix_size($tmp);
        }
       
$count++;
    }
}
closedir(
$handle);

//    done reading, sort the filenames alphabetically, shade these lines if you want no sorting
asort(
$pics);
reset(
$pics);

        // loop over images
        //for (
$f=0;$f<=sizeof($pics)-1;$f++){
        foreach (
$pics as $f=>$val) {
            echo \"<a href=\\"
full/$pics[$f]\\" target=\\"_blank\\"><img src=\\"thumbs/$pics[$f]\\" hspace=\\"5\\" vspace=\\"5\\" border=\\"0\\"><br>$size[$f]</a>\";
            // make linebreaks every 2 times!
            //
$isbr = strpos((($f+1)/2),\".\");
            //    if (!
$isbr){echo \"<br>\";}

        }


//if (false===(
$tmp=filesize(\"full/$file\"))) {
// die(\"1\");
//
$size[$count] = \"error reading size\";
//}
//else {
// die(\"2\");
//
$size[$count] = fix_size($tmp);
//}

echo \"\$pics = \"; print_r(
$pics); echo \"<br>\n\";
echo \"\$size = \"; print_r(
$size); echo \"<br>\n\";
asort(
$pics);
reset(
$pics);
echo \"\$pics = \"; print_r(
$pics); echo \"<br>\n\";
?>

Andy

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Ya, I wanted it above the foreach loop, after the closedir().

He has: 1,758 posts

Joined: Jul 2002

sorry i havent had a chance to continue with this... i'm up to my eyeballs at the mo... i'll get back to you soon i promise.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.