/    Sign up×
Community /Pin to ProfileBookmark

random image help

I have the following Random image PHP script, but it is not working.
“images/falehafez” is the directory of my images.
Please help me, and I’m not really a pro on PHP so please don’t use professional language. THANK YOU.

Starts:
<?
// #### RANDOM PIC #########################################

$thumbstring = ”;
$file_dir=”images/falehafez”; // DIRECTORY WITH THE PICS

$f_type=”.jpg”; // FILE EXTENSION YOU WISH TO DISPLAY

$dir=opendir(images/falehafez);
while ($file=readdir($dir))
{
if ($file != “.” && $file != “..”)
{
$extension=substr($file,-3); // THIS DIGIT MUST MATCH THE NUMBER OF CHARACTERS SPECIFIED IN THE FILE EXTENSION ABOVE
if($extension == $f_type)
{
$thumbstring .= “$file|”;
}
}
}
srand((double)microtime()*1000000);
$arry_txt = explode(“|” , $thumbstring);
echo “<img src=””images/falehafez”/”.$arry_txt[rand(0, sizeof($arry_txt) -1)].””>

Ends

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrAug 23.2004 — Try this.
[code=php]
<?php
// #### RANDOM PIC #########################################

$thumbstring = '';
$file_dir = 'images/falehafez'; // DIRECTORY WITH THE PICS
$f_type = '.jpg'; // FILE EXTENSION YOU WISH TO DISPLAY
if(($dir = @opendir($file_dir) !== false)) {
while ($file = @readdir($dir)) {
if ($file != '.' && $file != '..') {
$extension = substr($file, -4); // THIS DIGIT MUST MATCH THE NUMBER OF CHARACTERS SPECIFIED IN THE FILE EXTENSION ABOVE
if($extension == $f_type) {
$thumbstring .= $file . '|';
}
}
}
}
srand((double)microtime()*1000000);
$arry_txt = explode('|' , $thumbstring);
echo '<img src="' . $file_dir . '/' . $arry_txt[rand(0, count($arry_txt) -1)] . '" />';
?>
[/code]


Note: I'm not on my computer, so I can't test this out.
Copy linkTweet thisAlerts:
@aytakinauthorAug 23.2004 — Thanks for your help, but it didn't work.
Copy linkTweet thisAlerts:
@Paul_JrAug 24.2004 — Did you get any error messages or anything?
[code=php]
<?php
$thumb_string = array();
$img_dir = '/path/to/images';
if(is_dir($img_dir)) {
if(@opendir($img_dir)) {
while(($file = @readdir($img_dir)) !== false) {
if($file != '.' && $file != '..' && substr($file, -4) == '.jpg')
$thumb_string[] = $file;
}
}
}
}
$rand_img = $thumb_string[rand(0, count($thumb_string) - 1)];
echo '<img src="' . $img_dir . '/' . $rand_img . "' />';
?>
[/code]

Again, not on my computer, so I can't test it.
Copy linkTweet thisAlerts:
@aytakinauthorAug 24.2004 — It again didn't work, but thanks.
Copy linkTweet thisAlerts:
@pyroAug 24.2004 — You could try [url=http://www.alistapart.com/articles/betterrotator/]this one[/url].
Copy linkTweet thisAlerts:
@aytakinauthorAug 27.2004 — Thanks,

but i'm looking for something that takes the pic from a directory, i have too many pics to add a link for each one.
Copy linkTweet thisAlerts:
@rrfuertesNov 13.2011 — Try to use this. This code selects three unique random images from database (which is only the name of the picture) then it will output it on an html by comparing the name of pictures to the pictures in your directory.

[code=php]
<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("dbase1",$db);
$range_result = mysql_query( " SELECT MAX(id) AS max_id , MIN(id) AS min_id FROM image1 ");
$range_row = mysql_fetch_object( $range_result );
$random1 = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM image1 WHERE id >= $random1-2");
$img = mysql_fetch_array($result);
$img1 = $img + 1;
$img2 = $img + 2;
?>[/code]

[code=html]
<html>
<body>
<div>
<li id="1">
<?php
echo '<img src='. "index3_files/{$img['name']}.jpg". ' alt="" height="438" width="700">';
?>
<p><span><?php echo $img['caption']; ?></span></p>
</li>
<li id="2">
<?php
echo '<img src='. "index3_files/{$img1['name']}.jpg". ' alt="" height="438" width="700">';
?>
<p><span><?php echo $img1['caption']; ?></span></p>
</li>
<li id="3">
<?php
echo '<img src='. "index3_files/{$img2['name']}.jpg". ' alt="" height="438" width="700">';
?>
<p><span><?php echo $img2['caption']; ?></span></p>
</li>
</ul>
</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@TecBratNov 15.2011 — I have the following Random image PHP script, but it is not working.

"images/falehafez" is the directory of my images.

Please help me, and I'm not really a pro on PHP so please don't use professional language. THANK YOU.

Starts:

<?

// #### RANDOM PIC #########################################

$thumbstring = '';
$file_dir="images/falehafez"; // DIRECTORY WITH THE PICS

$f_type=".jpg"; // FILE EXTENSION YOU WISH TO DISPLAY

$dir=opendir(images/falehafez);
while ($file=readdir($dir))
{
if ($file != "." && $file != "..")
{
$extension=substr($file,-3); // THIS DIGIT MUST MATCH THE NUMBER OF CHARACTERS SPECIFIED IN THE FILE EXTENSION ABOVE
if($extension == $f_type)
{
$thumbstring .= "$file|";
}
}
}
srand((double)microtime()*1000000);
$arry_txt = explode("|" , $thumbstring);
echo "<img src=""images/falehafez"/".$arry_txt[rand(0, sizeof($arry_txt) -1)]."">


Ends[/QUOTE]


Looks like the error is in the last statement, the "echo". And I think that's what Paul Jr. was working on.

I think it should be like this:[code=php]echo '<img src="images/falehafez/'.$arry_txt[rand(0, sizeof($arry_txt) -1)].'">';[/code]
×

Success!

Help @aytakin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...