php – 名称中的空格正在打破下载?

前端之家收集整理的这篇文章主要介绍了php – 名称中的空格正在打破下载?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我有这个代码,允许用户下载一首歌
  1. $file = DIR_DOWNLOAD . $download_info->row['filename'];
  2. $mask = basename($download_info->row['mask']);
  3. $mime = 'application/octet-stream';
  4. $encoding = 'binary';
  5.  
  6. if (!headers_sent()) {
  7. if (file_exists($file)) {
  8. header('Pragma: public');
  9. header('Expires: 0');
  10. header('Content-Description: File Transfer');
  11. header('Content-Type: ' . $mime);
  12. header('Content-Transfer-Encoding: ' . $encoding);
  13. header('Content-Disposition: attachment; filename=' . ($mask ? $mask : basename($file)));
  14. header('Content-Length: ' . filesize($file));
  15. $file = readfile($file,'rb');

问题是,如果歌曲中有空格像sinsita happy 1 SONIFI.mp3那么用户只会下载一个名为sinsita的文本文件…任何想法如何解决这个问题

您必须在内容处置中引用文件名:
  1. header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');

编辑:现在这也意味着如果文件名包含引号;然后你必须逃避这句话.所以你的代码现在看起来像这样:

  1. header('Content-Disposition: attachment; filename="' . str_replace('"','\\"',($mask ? $mask : basename($file))) . '"');

猜你在找的PHP相关文章