In this line of your code $url = str_replace(' ','',$url);
returns a random url from one of the txt
files in the pasta
directory, such as www.site.com/nome/juliana.html
Soon after you use file_get_contents;
whose purpose is to read the contents of a file. But you're trying to read the contents of a url file_get_contents($url);
since $url
returns for example www.site.com/nome/juliana.html
and this will only generate an error and nothing more.
Warning: file_get_contents(www.site.com/nome/juliana.html):
failed to open stream: No such file or directory in .....
So remove this line from your code that is good for nothing but generate Warning
.
If you already have a random url to do the redirect, I did not understand why the list of urls
<li class="ohyeah">
.............
.............
</li>
You should have your reasons there and therefore to do this you can use the code below.
$conteudoLista="";
$arr="";
$i=1;
$pasta = 'pasta/';
$diretorio = dir($pasta);
while($arquivo = $diretorio -> read()){
if (file_exists($pasta.$i.".txt")) {
$f = file($pasta.$i.".txt");
foreach($f as $item){
$arr .= $item .",";
$strDir = str_replace(".html","",$item);
$nome = substr($strDir, 18);
$conteudoLista .="<a href='http://".$item."'>".$nome."</a><br>";
$conteudoLista = preg_replace( "/\r|\n/", "", $conteudoLista );
}
}
$i=$i+1;
}
$diretorio -> close();
$enderecos = (substr($arr,0,-1));
$partes = explode(',',$enderecos);
$qtd = count($partes);
$numRnad = (rand(1,$qtd));
$result=$partes[$numRnad];
echo "<li class=\"ohyeah\">".$conteudoLista."</li>";
echo "<br>";
echo "link random : ".$result;
Explaining:
$pasta = 'pasta/'
- relative directory path
dir()
- With this function, we will read the directory folder and, using the read () method, we will list all the files in this directory.
$diretorio = dir($pasta)
- read the directory
while($arquivo = $diretorio -> read())
- iteration to browse the files by listing them with the read () method
if (file_exists($pasta.$i.".txt"))
- check to see if the file exists
foreach($f as $item)
- iteration where $ item receives the value of each array item
$arr .= $item .","
- concatenating each item separated by comma
str_replace
- to remove (replace) the .html extension of urls
substr($strDir, 18)
- Extract a part of a string - returns the part of the url from position 18 to the end, that is, delete - www.site.com/nome/ - which is common to all urls
preg_replace( "/\r|\n/", "", $conteudoLista )
- performs a search for a regular expression and replaces it - \ n line break and \ r carriage return
substr($arr,0,-1)
- removing last comma from $ arr
$partes = explode(',',$enderecos)
- Let's separate string $ addresses in all comma occurrences
$qtd = count($partes)
- get the number of elements that make up the array $ parts
$numRnad = (rand(1,$qtd))
- generates a random number between 1 and the amount of array elements $ parts
$result=$partes[$numRnad]
- returns the value of the position $ numRnad of the array $ parts
$diretorio -> close()
- closed reading
Documentation used