This is quite simple to be done, there are N way to do this.
First you need to get the files, the list of files you have in the folder, in this case assuming that everything is inside of /pasta/
and are in .txt
could use the glob
to get the paths of the files and then select one of them using some generator.
$Arquivos = glob('pasta/*.txt');
$ArquivoEscolhido = $Arquivos[random_int(0, count($Arquivos) - 1)];
Okay, we use the random_int
to select one of the files, the $ArquivosEscolhidos
will have the path of one of the files, which was selected randomly.
Now just read the files, in this case using the file()
and select one of the lines, could do:
$Linhas = file($ArquivoEscolhido);
$LinhaEscolhida = $Linhas[random_int(0, count($Linhas) - 1)];
Ready, now you have selected a line and can display using echo $LinhaEscolhida
, for example or if you want to add header()
see this.
The random_int
is only available in PHP 7 and above. You could also use Mersenne Twister, via mt_rand
or shuffle
, or use an LFSR/LGC via rand
, if you use PHP 7 or lower. You can also use scandir
with some changes in the code.
Marcelo, put the code you are trying to use to create this task, this way people can help you find the problem because from what it seems you are asking to do for you, and it will not happen...
– RFL
And what do you mean by "access this URL"? PHP should make a request for the page or the user should be redirected?
– Woss
@Andersoncarloswoss, redirected to url.
– Marcelo