.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   a different sig each time (http://forums.bots-united.com/showthread.php?t=146)

Pierre-Marie Baty 12-05-2004 15:03

Re: a different sig each time
 
oh yeah, I'm interested as well :)

that's teh way I'm learning PHP: by reading at other ppl's code...

[NvT]_KaszpiR_ 12-05-2004 16:45

Re: a different sig each time
 
once i was using crontab :D
then i used php genrated image
then the .php links were forbidden because it could return dangerouns output so i changed it to writ the .jpg file a diff file (well more like crontab script in fact)

now i use static jpg cause i got used to it - easier to find my own posts

FrostyCoolSlug 12-05-2004 17:48

Re: a different sig each time
 
ok, here we go then :)
This script is much longer than the others, but does what its told :)

PHP Code:

<?php

$filepath 
"images/";
$extentions = Array("PNG""JPG""GIF");

// Open the Directory and clear the files array (JIC)
$handle = @opendir($filepath) or die("");
$files = Array();

// Loop through all the files in the directory
while (false !== ($file readdir($handle))) {
    
// Make sure they are not directories
    
if (!is_dir($file)) {
        foreach (
$extentions as $m00) {
            
// Case insencitive comparison of file extention and
            // Allowed extention
            
if (strcasecmp(getext($file), $m00)) {
                
// Positive Match, add the file to the array
                
$files[] = $file;
                
// Jump out the foreach and tell the while to continue
                
continue 2;
            } 
        } 
    } 

// Make sure there are some files..
if (!count($files)) {
    
// No Valid files were found, kill self.
    
return;
} else {
    
// Choose a random image (the keys are numbers, so can rand them)
    // Use count() -1 because count() starts at 1 and not 0 
    
$img rand(0count($files) - 1);

// Output the file.
readfile($filepath $files[$img]); 

// Function used for fetching extentions..
function getext($file)
{
    
$file explode("."$file);
    return 
$file[count($file) - 1];


?>

I've commented it well, so you know what i'm doing. I've tested it, and know it works :D

If you have any questions, gimme a shout :)

Pierre-Marie Baty 12-05-2004 19:14

Re: a different sig each time
 
hehe w00t thanks

I've made it a lil bit shorter :)

don't forget to close the directory handle in your code btw...
Code:

<?php
 
$handle = opendir (getcwd ()) or die ();
$files = Array ();
$file_count = 0;
 
// loop through all the files in the directory
while (($file = readdir ($handle)) !== false)
{
  if (is_dir ($file))
          continue; // discard directories
 
  $ext = strstr ($file, "."); // get the file extension
 
  // is this file of one of the allowed extensions ?
  if ((strcasecmp ($ext, ".png") == 0)
          || (strcasecmp ($ext, ".jpeg") == 0)
          || (strcasecmp ($ext, ".gif") == 0))
  {
          $files[$file_count] = $file; // yes it is, add the file to the array
          $file_count++; // we know now one image more
  }
}
 
closedir ($handle); // finished, don't forget to close directory handle
 
// did we find no image ?
if ($file_count == 0)
  return; // if so, don't output anything at all.
 
// output a random image file among those in the array
readfile ($files[rand (0, $file_count - 1)]);
 
?>


SoUlFaThEr 12-05-2004 20:10

Re: a different sig each time
 
how in the world could a simple Joe like me get this one going? i like that kind of stuff!

Pierre-Marie Baty 12-05-2004 23:40

Re: a different sig each time
 
Well, just copy what's inside the code tags and paste it in your notepad, then save the file and name it "sig.php" or whatever. Then upload it to your server in the directory where you put your signatures, and instead of using [ img]http://soulfathermaps.de/images/mysig.jpg[ /img] use [ img]http://soulfathermaps.de/images/sig.php[ /img]
That should be enough
Ensure the directory where your sig is has read access allowed to anybody.

SoUlFaThEr 12-05-2004 23:45

Re: a different sig each time
 
sounds real easy :)

and how do i make it select 4 pics designed just for this purpose? and not the whole bank of pics?

Huntkillaz 13-05-2004 00:12

Re: a different sig each time
 
to my understanding (php is one of my things in the the things to learn list :()

if u upload the 4 photo's\images and the sig in the same folder to the server
and link upto that sig.php script it will automatically choose from that 4 becoz there is no other in that folder

FrostyCoolSlug 13-05-2004 02:55

Re: a different sig each time
 
Quote:

$ext = strstr ($file, "."); // get the file extension
You gotta be careful with this, some people like adding extra .'s to their filenames (god knows why), so if someone had a file called my.pic.jpg, $ext would be pic.jpg, which wont work in comparison with the others.

Quote:

while (($file = readdir ($handle)) !== false)
I'm not sure why that was changed, i used what was recommended @ php.net as the 'right' way to do it.

Mine was designed to be more customisable, rather than 'just work', so you could select the folder easily, and simple add , "ext" to add an extention to the list..

What i've done here, is taken the speed of yours, and the customisability of mine, and combined them, and got the following :p

PHP Code:

<?php

$target 
getcwd() . "/images";
$extentions = Array("PNG""JPG""GIF");
$files = Array();
$file_count 0;

$handle opendir($target) or die ();
while ((
$file readdir($handle)) !== false) {
    if (
is_dir ($file))
        continue;
    
$tmp explode("."$file);
    
$ext $file[count($file) - 1];
    unset(
$tmp);
    foreach (
$extentions as $m00) {
        if (
strcasecmp($ext$m00)) {
            
$files[$file_count++] = $file;
            continue 
2;
        } 
    } 


closedir ($handle);

if (!
$file_count)
    return;

readfile ($files[rand (0$file_count) -1]);

?>

Its not commented, but you should know what it does by now ;)

Pierre-Marie Baty 13-05-2004 04:34

Re: a different sig each time
 
@Frosty: About the "rand (0, $file_count - 1)", I suppose the array indexing works just like in C ?
Let's say we have an array of 3 elements (hence $file_count = 3)
we have
array[0] -> first element
array[1] -> second element
array[2] -> third element (and no array[3], which is out of bounds)
is that right ?
so if I want to pick a random number between 0 and 2 inclusively, I call rand (0, $file_list - 1)... well that's how I see things, but you know PHP better than me so... ???:(

@SF: with this script you have to arrange your pics into subdirectories, because it takes the whole image list.
If you want a script that would output only a selected set of images randomly, you can write something like this:
Code:

<?php

$files = Array("image1.gif", "image2.jpeg", "image3.png"); // list of files
$file_count = 3; // number of files to choose from

// take one file randomly among the list and send it away
readfile ($files[rand (0, $file_count - 1)]);

?>



All times are GMT +2. The time now is 22:08.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.