Php 嵌入随机命名的MP3

Php 嵌入随机命名的MP3,php,html,random,mp3,Php,Html,Random,Mp3,这是我的密码: <embed src="/sound/lowyourchicken.mp3" width="140" height="40" autostart="true" loop="TRUE"> </embed> 我希望.mp3的src考虑到/sound/目录中有许多随机命名的.mp3文件,并在每次打开页面时随机选择一个。有我的线索吗 我的服务器启用了PHP,但我想让它尽可能简单。这应该可以做到: $files = glob("/path/to/dire

这是我的密码:

<embed src="/sound/lowyourchicken.mp3" 
width="140" height="40" autostart="true" loop="TRUE"> 
</embed> 

我希望.mp3的src考虑到
/sound/
目录中有许多随机命名的.mp3文件,并在每次打开页面时随机选择一个。有我的线索吗

我的服务器启用了PHP,但我想让它尽可能简单。

这应该可以做到:

$files = glob("/path/to/directory/*.mp3");
$random = array_rand($files)
然后这样做:

<embed src="<?php echo $random ?>" 
width="140" height="40" autostart="true" loop="TRUE"> 
</embed> 

array\u rand返回它选择的随机索引,因此您需要执行以下操作:

<embed src="<?php $files[ $random ] ?>"
试试这个:
它会起作用的,我使用了在答案中找到的原始代码,并通过在
$random=array_rand()中添加
array($files)
进行了一些调整变量语句

您首先需要像这样将PHP代码放入主体中

<?php
$files = glob("assets/songs/SayYesToLove/*.mp3");
$random = array_rand(array($files));
?>

接下来,在主体中的php代码之外添加此代码

<embed src="<?php echo $files[$random]; ?>" width="140" height="40" autostart="true" loop="TRUE">
</embed>

到目前为止您有什么代码?你能把它贴出来吗?我认为你不能只用HTML就可以做到这一点。在发送到客户端之前,您可能需要服务器端代码来选择一个随机文件名并将其注入HTML。如果在HTML中不可能,有人能用这个函数向我指出一些php吗?当我将php放在标题中并嵌入正文时,我在页面上的mp3应该放在的地方会出现一个奇怪的白色框,源代码如下:@John:我认为@bpeterson76在echo语句中缺少了一个
$
。谢谢大家,我确实尝试过在random之前放入$,但它只是将输出从src=“random”更改为src=”“我将php放在实际index.php html的头部,我应该把它放在哪里?你在用标签包裹顶部吗?如果周围没有标记,Php将无法工作。好的,我将Php从头部移动到身体,以使事情更清楚,下面是它的外观: