PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Function for random image from directory
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Function for random image from directory

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 23h50   #1
howdy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Function for random image from directory

This little program reads a directory of files, and prints out a
random thumbnail from that directory.

I cleaned it to shorten as much as possible.

I have two questions:
1) How can I make it shorter and remain readable. (I know there must
be faster methods and shorter code).

2) I want to require this file at the beginning of a page, get my
directory array just once, let some html pass and then run the
function later a few times. When I tried that, I could not get the
$filesread array to carry over to the function (a scope issue i
guess).
I just don't want to repeat reading the directory files into the array
every time I call the function.

For reference this file the working file is at: http://styletattoo.com/new.php.
Hopefully the code will post verbatim/
-----------------------------------------------------------------------------------------------
<?
function RandomStencil()
{
$path = $_SERVER['DOCUMENT_ROOT'];
$fullpath = $path . "/stencil/thumb/";

//-----open the directory
$d=$fullpath; #define which dir you want to read
$dir = opendir($d); #open directory

//-----read all files into an array while removing the 'th.gif' from
the end of each filename
while ($f = readdir($dir))
{$filesread[] = str_replace("th.gif", "", $f);}

//-----sort and shift off the two . and .. files
sort($filesread,SORT_NUMERIC);
$trash=array_shift($filesread);
$trash=array_shift($filesread);

//-----choose a random stencil and print it with link to big stencil
page
$countera = count($filesread);
srand ((double) microtime() * 1000000);
$randomstencil = rand(0,countera-1);
$randstenc = $filesread[$randomstencil];
print ("<a href='stencil/stencil.php?image=" . $randstenc . "'
title=''><img class='navlastimg' border='0' src='stencil/thumb/" .
$randstenc . "th.gif' alt='' /></a>");
}
?>
-----------------------------------------------------------------------------------------------
  Réponse avec citation
Vieux 26/02/2008, 05h07   #2
coolhand2@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Function for random image from directory

On Feb 25, 3:50pm, howdy <i...@mauicomputer.com> wrote:
> This little program reads a directory of files, and prints out a
> random thumbnail from that directory.
>
> I cleaned it to shorten as much as possible.
>
> I have two questions:
> 1) How can I make it shorter and remain readable. (I know there must
> be faster methods and shorter code).
>
> 2) I want to require this file at the beginning of a page, get my
> directory array just once, let some html pass and then run the
> function later a few times. When I tried that, I could not get the
> $filesread array to carry over to the function (a scope issue i
> guess).
> I just don't want to repeat reading the directory files into the array
> every time I call the function.
>
> For reference this file the working file is at:http://styletattoo.com/new.php.
> Hopefully the code will post verbatim/
> -----------------------------------------------------------------------------------------------
> <?
> function RandomStencil()
> {
> $path = $_SERVER['DOCUMENT_ROOT'];
> $fullpath = $path . "/stencil/thumb/";
>
> //-----open the directory
> $d=$fullpath; #define which dir you want to read
> $dir = opendir($d); #open directory
>
> //-----read all files into an array while removing the 'th.gif' from
> the end of each filename
> while ($f = readdir($dir))
> {$filesread[] = str_replace("th.gif", "", $f);}
>
> //-----sort and shift off the two . and .. files
> sort($filesread,SORT_NUMERIC);
> $trash=array_shift($filesread);
> $trash=array_shift($filesread);
>
> //-----choose a random stencil and print it with link to big stencil
> page
> $countera = count($filesread);
> srand ((double) microtime() * 1000000);
> $randomstencil = rand(0,countera-1);
> $randstenc = $filesread[$randomstencil];
> print ("<a href='stencil/stencil.php?image=" . $randstenc . "'
> title=''><img class='navlastimg' border='0' src='stencil/thumb/" .
> $randstenc . "th.gif' alt='' /></a>");
> }
> ?>
> -----------------------------------------------------------------------------------------------


The simple one that I see, is to take


$path = $_SERVER['DOCUMENT_ROOT'];
$fullpath = $path . "/stencil/thumb/";

//-----open the directory
$d=$fullpath; #define which dir you want to read
$dir = opendir($d); #open directory

And change that to:

$dir = opendir( $_SERVER['DOCUMENT_ROOT'] . "/stencil/thumb" );

And move the while loop for reading the directory to be one line:

while ($f = readdir($dir)) $filesread[] =
str_replace("th.gif", "", $f);

Also, it looks like, later in your print statement, you're just
stuffing that th.gif right back on.
The following is only if you expect there to be no missing numbers:

while( $f = readdir( $dir ) ) $filesread[] = $f;

You also don't need to store those things that you're popping off the
top of the array,

sort($filesread,SORT_NUMERIC);
array_shift($filesread);
array_shift($filesread);

No need to waste memory there, or with the counter for that matter.

$countera = count($filesread);
srand ((double) microtime() * 1000000);
$randomstencil = rand(0,countera-1);

Becomes

srand ((double) microtime() * 1000000);
$randomstencil = rand( 0, count($filesread) - 1 );

So you're final code would look something like:
<?
function RandomStencil()
{
//-----open the directory
$dir = opendir( $_SERVER['DOCUMENT_ROOT'] . "/stencil/
thumb/" ); #open directory

//-----read all files into an array while removing the 'th.gif' from
the end of each filename
while ($f = readdir($dir)) $filesread[] = $f;

//-----sort and shift off the two . and .. files
sort($filesread,SORT_NUMERIC);
array_shift($filesread);
array_shift($filesread);

//-----choose a random stencil and print it with link to big stencil
page
srand ((double) microtime() * 1000000);
$randomstencil = rand(0, count($filesread) - 1);
$randstenc = $filesread[$randomstencil];
print ("<a href='stencil/stencil.php?image=" . $randstencil .
"' title=''><img class='navlastimg' border='0' src='stencil/thumb/" ..
$randstenc . "th.gif' alt='' /></a>");
}
?>
----------------------------------------
And I probably missed more here (or made something unreadable for
someone as well) so I wouldn't be surprised if you got other people
saying different things.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h34.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12080 seconds with 10 queries