doutang7383 2017-03-01 15:39
浏览 477
已采纳

无法加载资源:net :: ERR_CONTENT_LENGTH_MISMATCH(php error)

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.

  • 写回答

1条回答 默认 最新

  • duandao1931 2017-03-03 23:19
    关注

    The issue to generate this error was within the PHP script I had.

    Where I had: header('Content-length: '.filesize($rand));

    Instead I needed: header('Content-Length: '.filesize($local_file));

    Since I don't know PHP that much, I assume using $local_file instead of $rand it would check the file size that was in the same directory as the files the script needed to pull.

    If you run into this issue and you have a header issue like mine that throws the same error code try using $local_file or here's the full header: header('Content-Length: '.filesize($local_file));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱