I'm having two issues, one being that I can't loop songs (I tried in the audio tag in the HTML), and two being: Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH.
Misc info: The files are hosted on a cPanel. I also have other javascript files in the HTML file that the audio tags rest in, sometimes when I reload it the error has issues with the javascript. CSS, PHP, and JS files are also called within the same HTML file (if it matters) Also everything is being transferred over HTTPS.
What I'm trying to accomplish is playing music on a webpage using a PHP script. This script pulls mp3, mp4, and m4a files from a directory without having to draw an array for convenience. My friend made me this PHP script but we can't seem to figure out the issues so I'm asking for help here because I don't fully understand PHP yet.
HTML code:
<audio autoplay="" loop="" preload="auto" id="music" src="https://example.com/audio/music.php">
<script type="text/javascript">
var audio = document.currentScript.parentElement;
audio.volume = 0.1;
</script>
</audio>
PHP file:
<?php
$list = new RecursiveDirectoryIterator(__DIR__);
$filetypes = array('mp3', 'mp4', 'm4a');
$files = array();
foreach(new RecursiveIteratorIterator($list) as $file) {
if (in_array(strtolower(array_pop(explode('.', $file))), $filetypes))
$files[] = $file;
}
$rand = $files[array_rand($files, 1)];
header('Content-Type:'.mime_content_type($rand));
header('Content-Disposition: filename="'.basename($rand).'"');
header('Content-length: '.filesize($rand));
readfile($rand);
?>
I've tested this in Firefox and I don't get this error: ( Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH ) and says at my example.com/audio/music.php file that is the issue. But the audio will also not loop. My friend has also tested this in Brave, and his version of Chrome but in my version of Chrome I get this issue.
I've asked a few people already and they can't seem to figure out why this is not working. I've looked in a few posts on here about similar issues and can't seem to fix it off of their suggestions.
I'm a bit of a noobie at PHP so if you can help guide me on how to fix it like where to add, replace, or delete code please tell me where.
Thanks for your time, hope we can get this to work.