php file reading help...
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 posted this at 17:03 — 29th April 2003.
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.
andy206uk posted this at 17:44 — 29th April 2003.
He has: 1,758 posts
Joined: Jul 2002
Thanks Mark! I'll test it now and see how I get on!
andy206uk posted this at 17:54 — 29th April 2003.
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 posted this at 04:07 — 30th April 2003.
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\";
}
}
?>
Mark Hensler
If there is no answer on Google, then there is no question.
andy206uk posted this at 08:14 — 30th April 2003.
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 posted this at 10:55 — 30th April 2003.
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.
andy206uk posted this at 11:42 — 30th April 2003.
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 posted this at 16:56 — 30th April 2003.
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.
andy206uk posted this at 19:01 — 30th April 2003.
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?
Mark Hensler posted this at 19:01 — 1st May 2003.
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.
andy206uk posted this at 08:14 — 2nd May 2003.
He has: 1,758 posts
Joined: Jul 2002
I just get number 2 again... Still good?
Mark Hensler posted this at 11:18 — 2nd May 2003.
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.
andy206uk posted this at 11:49 — 2nd May 2003.
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 posted this at 21:46 — 2nd May 2003.
He has: 4,048 posts
Joined: Aug 2000
Ya, I wanted it above the foreach loop, after the closedir().
andy206uk posted this at 12:27 — 7th May 2003.
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.