TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,966
  • Latest: safir45
Stats
  • Total Posts: 195,985
  • Total Topics: 21,321
  • Online today: 411
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 643
  • Total: 643

Display Random Files in a block

Started by gerrymo, October 22, 2006, 01:50:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

gerrymo

This script will display a random file (txt, html, htm, php) from a single directory.


<?php

$txt_array 
= Array();
$my_dir "XXXXX/";   //Replace it with your directory (don't delete the slash)
if ($dir = @opendir("$my_dir")) {

  while (($file readdir($dir)) !== false) { 
   if ($file != "." && $file != ".."  && !is_dir($my_dir.$file)) 
   {
   /* echo $my_dir.$file."<br>";  DEBUG show file list */
   $txt_array[] = $file;
   
   } 
  }  
  closedir($dir);
}

$random_number=rand(0count($txt_array)-1); 
$random_txt=$txt_array[$random_number];

//This code includes the text file file
include($my_dir.$random_txt);

//This code show a random image
// echo "<img src=\"".$my_dir.$random_txt."\" border=\"0\" alt=\"\">";
?>


Change XXXXX to the directory you want to display files from.

Create a new php file using the above code and upload to your server.

Make a php block and and use an include to call the file you just made into a block using

include('FULL URL/filename.php');


You're now good to go. Each time the page is loaded, it will display a random file from the directory you specified.

akulion


gerrymo

I do have to admit, It was sent to me by e-mail, I didnt write it.

akulion

no problem :up: its the sharing that counts more than anyhing else :D

BjornB

Hi,

I can't get this to work after upgrading to TP 9.7.1 and SMF 1.1.1. Randomfile works great as stand alone but SMF/TP wont include as before the upgrade.

Anyone know a fix for this?

This website is proudly hosted on Crocweb Cloud Website Hosting.