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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)