Shoutbox

Some PHP Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Some PHP Help (/showthread.php?tid=53276)

Some PHP Help by Val on 11-25-2005 at 03:49 AM

Hey guys im a total php newbie so im wondering how to make a script for random images in a dir. Like some people have in the avatars (i.e Chromy ;)) i thinks its easy to make so if someone could please give me kinda tutorial so i can do it myslef and no bother anyone.

Thanks,
ValSpy


RE: Some PHP Help by Ash_ on 11-25-2005 at 03:52 AM

use scan dir to scan for all gif files. (scandir and substr). then load them all into an array get a random namber thats not bigger then count($array); and use that image.

www.php.net to look up those two functions =)


RE: Some PHP Help by Val on 11-25-2005 at 03:56 AM


quote:
Originally posted by Ash_
use scan dir to scan for all gif files. (scandir and substr). then load them all into an array get a random namber thats not bigger then count($array); and use that image.

www.php.bet to look up those two functions =)
www.php.bet? i think its .net anyways i have no clue what you just said after all im a total php newbie
RE: Some PHP Help by Ash_ on 11-25-2005 at 04:07 AM

quote:
Originally posted by ValSpy
quote:
Originally posted by Ash_
use scan dir to scan for all gif files. (scandir and substr). then load them all into an array get a random namber thats not bigger then count($array); and use that image.

www.php.bet to look up those two functions =)
www.php.bet? i think its .net anyways i have no clue what you just said after all im a total php newbie


lol, you aint gonna learn if i give you the code are you?

code:
<?php
$dir    = '/tmp';
$files1 = scandir($dir);

print_r($files1);
?>

that code gets and displays all contents of a folder.

code:
echo substr('abcdef', -1, 1); // f

prints f.

so you could go.

code:
$dir    = '/images';
$files1 = scandir($dir);

for ($x=0;$x<count($files1);$x++){
if (substr($files1[$x], -1, 3) == 'jpg') {
$images[] = $files1[$x];
print_r ($images);
}
}


now all the images in the folder are in the $images array.

now we get a random number.

code:
$num = rand(0, count($files1));

all code.

code:
$dir    = '/images';
$files1 = scandir($dir);

for ($x=0;$x<count($files1);$x++){
if (substr($files1[$x], -1, 3) == 'jpg') {
$images[] = $files1[$x];
print_r ($images);
}
}
$num = rand(0, count($files1));
echo "<img src='$dir/$images[$num]' />


its not the cleanest way to write it but i should work (not at home at the moment)
[/code]
RE: Some PHP Help by Val on 11-25-2005 at 04:14 AM

thank you, but i should save it as a .php file right?


RE: Some PHP Help by Ash_ on 11-25-2005 at 04:15 AM

yup.


RE: Some PHP Help by Val on 11-25-2005 at 04:24 AM

quote:
Originally posted by Ash_
yup.

hmm.... whenever i try to do this i get this error:
---------------------------------------
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@valspy.louhabs.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
-----------------------------------
or this one
-----------------------------------

Fatal error: Call to undefined function: scandir() in /home2/louhabs/public_html/valspy/images/random.php on line 3
-----------------------------------
do you know how to solve this?
RE: Some PHP Help by Ash_ on 11-25-2005 at 04:27 AM

what version of php are you running.

create a new file text.php

put in it

code:
echo phpinfo();


RE: Some PHP Help by Val on 11-25-2005 at 04:35 AM

quote:
Originally posted by Ash_
what version of php are you running.

create a new file text.php

put in it

code:
echo phpinfo();


well i know how to do that, ive made one a while ago here it is: http://valspy.louhabs.com/php.php

version 4.3.10

EDIT: verstion to version, I hate you Lou! :P
RE: Some PHP Help by Mike on 11-25-2005 at 12:57 PM

I think scandir(); works only for PHP 5...


RE: Some PHP Help by -dt- on 11-25-2005 at 02:17 PM

quote:
Originally posted by Mike
I think scandir(); works only for PHP 5...
Thats correct.

in php4 you could use this function insted

code:
<code>function scandir2($dir){
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}
return $files;
}

</code>