1.放入以下函数到conf/FunctionsExt.php
//修正多附件字段
    function fixFile($data){
           if($data){
                $inputStr = $data;
                $segments = explode('||', $inputStr);
                $segments = array_filter($segments, function($item) {
                    return trim($item) !== '';
                });
                //提取标题和顺序
                $pathToTitle = [];  
                $pathOrderMap = [];  
                $order = 1;
                foreach ($segments as $segment) {
                    if (empty(trim($segment))) continue;
                    $parts = explode('|', $segment, 2); 
                    if (count($parts) === 2) {
                        $path = trim($parts[0]);
                        $title = trim($parts[1]);
                        $pathToTitle[$path] = $title;
                        $pathOrderMap[$path] = $order++;
                    }
                }
                
     
                //获取url字符串
                $quotedUrls = [];
                foreach ($segments as $item) {
                    $parts = explode('|', $item, 2); 
                    $url = trim($parts[0] ?? '');
                    if (!empty($url)) {
                        $quotedUrls[] = '"' . $url . '"';
                    }
                }
                
                $orders = 'addtime desc';
                $sql = ' litpic in ('.implode(',',$quotedUrls).') ';
                $fres = M('pictures')->findAll($sql,$orders);
                
                foreach($fres as $k=>$v){
                     $url = $fres[$k]['litpic'];
                     $fres[$k]['title'] = $pathToTitle[$url]; 
                     $fres[$k]['orders'] = $pathOrderMap[$url];
                }
                usort($fres, function($a, $b) {
                    return $a['orders'] - $b['orders'];
                });
               return $fres;
             }else{
                 return [];  
             }   
    }
2.前台HomeController.php在function jizhi_details($id){}中使用
//重构附件files字段

    if(array_key_exists('files',$details)){
        $this->files = fixFile($details['files']);
    }
3.在前台Common控制器添加下载处理函数
    function download() {
        if($_POST){
            $id = $this->frparam('fid',1,'POST');
            $file_name =$this->frparam('file_name',1,'POST');
            if(!$id||!$file_name){
               http_response_code(405);
               //echo "错误:参数缺失";
               exit; 
            }
            if (!preg_match('/^[\w\x{4e00}-\x{9fa5}\-]+$/u', $file_name)) {
            http_response_code(405);
            //echo "错误:文件名包含非法字符";
            exit;
           }
            $fres = M('pictures')->find(['id'=> $id]);
            if($fres){
                $file_url = $fres['litpic'];
                $now =date('Ymd_His', time());
                $download_name = $file_name.$now. '.' . $fres['filetype'];
                if (file_exists(APP_PATH.$file_url) && is_file(APP_PATH.$file_url)) {
                    $fileSize = filesize(APP_PATH.$file_url);
                            header('Content-Description: File Transfer');
                            header('Content-Type: application/octet-stream');
                            header('Content-Disposition: attachment; filename="' . basename($download_name) . '"');
                            header('Expires: 0');
                            header('Cache-Control: must-revalidate');
                            header('Pragma: public');
                            header('Content-Length: ' . $fileSize);
                    readfile(APP_PATH.$file_url);
                    exit;
                }else{
                    http_response_code(404); 
                    //echo "错误:文件不存在。";
                    exit; 
                }
                
            }else{
              http_response_code(404);  
              //echo "错误:文件不存在。";
              exit;    
            }
         
        }else{
          http_response_code(405);  
          exit;
        }
    } 

4.前端详情页模板使用
    {foreach $rjfile as $v}
        <li>
          {$v['id']}
          {$v['title']}
          {$v['filetype']}
          {$v['size']}
          {fun date('Y-m-d',$v['addtime'])}
         <a href="javascript:;" class="downf" fid="{$v['id']}" fname="{$v['title']}">下载</a>
        </li>
    {/foreach}
5.结合js实现文件下载
<script type="text/javascript">
 function download(id,filename){
  $('body').append(`<form method="post" target="_blank" action="/common/download.html" id="downloadForm"><input type="hidden" name="fid" value="${id}" required><input type="hidden"  name="file_name" value="${filename}" required></form>`);
  setTimeout(function(){
    $("#downloadForm").submit();
    setTimeout(function(){
        $("#downloadForm").remove()
        
    },1000)
  },500)
}
$(function(){
    $(".downf").each(function(){
        $(this).click(function(){
            var fid = $(this).attr("fid");
            var fname = $(this).attr("fname");
           download(fid,fname) 
        })
    })
    
})
</script>

标签: 极致cms

您需要先登录才能发表评论。