i am trying to give random name to my file name using this code but how do i provide file path and name so it works?
this is my file path
<script type="text/javascript" src="../../js/file.js"></script>
and here's my code
<?php
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
echo random_string(50);
?>
this out put wil be like this " i40q6jjmvib6tb36skrutcitok1gxfkprz65e50xsiembjov0b "
and i want it to be like this on my file
<script type="text/javascript" src="../../js/i40q6jjmvib6tb36skrutcitok1gxfkprz65e50xsiembjov0b.js"></script>
<?php
function random_string($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return "js-".$key.".js"; // Change here
}
echo random_string(50);
?>
And in your htaccess
, write condition for rewriting /js\-[a-z0-9]*\.js/
to file.js
First rename your file.js
file
$random_name = random_string(50);
$dir = '/your_path/';
$files = scandir($dir, 1);
rename($dir."/".$files[0], $dir."/".$random_name);
And then use this random name in html
<script type="text/javascript" src="../../js/<?php echo $random_name;?>"></script>
Above both code should be in same .php file