-1

i want to get random file from includes folder.

<?php
$text = file_get_contents("includes/searchterms.txt");
$text = str_replace("-", " ", $text);
$text=explode('[searchterms]:',$text);
foreach ($text as &$value){
    $value=rtrim($value);
}

$out=array_slice($text,-100);
$out=array_reverse($out);
$out=array_unique($out);

$latest="";
foreach ($out as $value) {
    $latest .= '<a href="';
    $value1 = str_replace(" ", "-", $value);
    $latest .= "/watch/$value1/";
    $latest .= "\">".htmlspecialchars($value)."</a>";
}
echo"$latest";
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
usefpf
  • 21
  • 2
  • 1
    Ok, so what is wrong with the code you have? Is it giving you errors? – RiggsFolly Oct 18 '18 at 12:56
  • 1
    Welcome, to improve your experience on SO please read how to ask an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [TAKE THE TOUR](http://stackoverflow.com/tour) – RiggsFolly Oct 18 '18 at 12:58
  • the code work fine i just want to pick rondom file evry time the page loaded – usefpf Oct 18 '18 at 13:01
  • can you just show me what to change and ill change it pleas – usefpf Oct 18 '18 at 13:06
  • Ok I found a DUP if anyone is interested. https://stackoverflow.com/questions/4478783/select-random-file-from-directory So all you needed to do was do a Google Search – RiggsFolly Oct 18 '18 at 13:08
  • RiggsFolly if you cant help or dont want to help let others do plz, i did the search on google i dont find what i want thats why i isk here – usefpf Oct 18 '18 at 13:20
  • I did help, I gave you a link to a perfectly good answer to a very similiar question, here on Stack Overflow – RiggsFolly Oct 18 '18 at 13:21

1 Answers1

0

You could use glob with a wildcard, to return a array of files inside your given directoy. With rand, you could pic a random index, inside this array.

$files = glob('includes/*');
$file = $files[rand(0, count($files) - 1)];
$text = file_get_contents($file);
Philipp
  • 15,377
  • 4
  • 35
  • 52