View Full Version : need Help with random php script
Marquis
05-02-2018, 04:04 PM
Hello
I use this script to random include file from a directory. I must declare all files that i want to choose from.
Is it possible to change this that it includes files from a directory without that i must declare each single file?
<?php
srand();
$files = array("/1/file1.txt", "/1/file2.txt", "/1/file3.txt", "/1/file4.txt","/1/file5.txt","/1/file6.txt");
$rand = array_rand($files);
include ($files[$rand]);
?>
Thank you, please excuse my bad english
See the php glob() function to get an array of the files within a folder.
Marquis
05-02-2018, 05:51 PM
Thank you for your answer but i am not a coder... i don't have idea how to do what
Actually, you became a coder when you created/edited your file with the file names in it, regardless of how proficient you are.
The php.net documentation for the glob() function defines what the input parameters are and what result the function returns, along with some examples.
One of the fun points about programming is, you can try things and immediately see the result, thereby allowing you to learn if what you tried was correct or not.
You need to make an attempt at solving this yourself and if you cannot solve it or get errors, post your code and the error or incorrect result, and someone will try to help you with the problem.
jscheuer1
05-03-2018, 10:35 PM
OK, the problem with glob for this is that it requires the relative path. What you have is perhaps an absolute network path:
"/1/file2.txt"
If that means (network), we go from the root of the domain, to a folder named 1, and look for .txt files there. If it's already relative, it means we go to a folder named 1 in the current directory and look for .txt files there.
Glob can only do the latter. Do you know which you have?
styxlawyer
05-04-2018, 09:13 AM
As long as the text files are all stored in the same directory and that directory doesn't contain any other files, then 'readdir()' would be the way to go.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.