du060334 2016-11-30 23:39
浏览 76
已采纳

preg_replace和file_get_contents没有读取阿拉伯文件名

First it was the preg_replace not reading Arabic characters for file name so when downloading it shows .pdf only so I added the arabic characters to the preg_replace to get the file name as is. Now the file name shows as "اÙÙÙاعداÙعربية.pdf"

I'm not sure if anything wrong with my code

public function download($id){
    $toReturn = study_material::where('id',$id)->first();
    if(file_exists('uploads/material/'.$toReturn->material_file)){
        $fileName = preg_replace('/[^أ-يa-zA-Z0-9-_\.]/','',$toReturn->material_title). "." .pathinfo($toReturn->material_file, PATHINFO_EXTENSION);
        header("Content-Type: application/force-download");
        header('Content-Type: text/html; charset=utf-8');
        header("Content-Disposition: attachment; filename=" . $fileName);
        echo file_get_contents('uploads/material/'.$toReturn->material_file);
    }
    exit;
}
  • 写回答

1条回答 默认 最新

  • dongxian3418 2016-12-01 20:59
    关注

    preg_replace does not support multibyte strings, so multi-byte letters are understood by the function as separate letters. You will need to use a multi-byte compatible function like mb_ereg_replace.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度