dongtanxi5676756 2016-09-09 10:54
浏览 29
已采纳

PHP - 服务文件出错

Here is my code. I wrote this to serve mp3, images and videos. Mp3 files are received and working well. Images and videos are received but corrupted. I'm new to php.

<?php
if( !empty( $_GET['type']|| $_GET['name']|| $_GET['ext'] ) ) {
  // check if user is logged
  if(true) {
    $type = preg_replace( '#[^-\w]#', '', $_GET['type'] );
    $name = $_GET['name'] ;
    $file = "{$_SERVER['DOCUMENT_ROOT']}/cont/{$type}/{$name}";
    echo $file;

    if (file_exists($file)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="'.basename($file).'"');
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      readfile($file);
      exit;
    }
  }
}
die( "ERROR: invalid file or you don't have permissions to download it." );

Can you help me?

  • 写回答

1条回答 默认 最新

  • duanjue6575 2016-09-09 12:22
    关注

    There is an echo $file; before the actual response that doesn't belong there. Besides the text itself, PHP also produces a Warning on the first call to header(). Both these arrive in the final content before the file header and makes the data received by the browser unrecognizable by most file readers.

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?