teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

バージョン情報追加

2019/12/09 09:05

投稿

Rebecca
Rebecca

スコア11

title CHANGED
File without changes
body CHANGED
@@ -50,7 +50,8 @@
50
50
 
51
51
  ### 補足情報(FW/ツールのバージョンなど)
52
52
 
53
- Mac OS
53
+ Mac OS Mojave バージョン 10.14.6
54
+ Safari バージョン13.0.3
54
55
  Vagrant + CentOS6.x
55
56
  cakePHP2.x
56
57
 

4

やってみたこと追記

2019/12/09 09:05

投稿

Rebecca
Rebecca

スコア11

title CHANGED
File without changes
body CHANGED
@@ -52,4 +52,23 @@
52
52
 
53
53
  Mac OS
54
54
  Vagrant + CentOS6.x
55
- cakePHP2.x
55
+ cakePHP2.x
56
+
57
+ ### 追記 PHPでやってみました
58
+ ``` PHP
59
+ <?php
60
+ $file_path = [サーバ上のディレクトリ/ファイル名];;
61
+ $file_name = 'ほげほげ.docx';
62
+
63
+ // HTTPヘッダ設定
64
+ header('Content-Type: application/octet-stream');
65
+ header('Content-Length: '. filesize($file_path));
66
+ //header('Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($file_path));
67
+ //ダウンロードの指示・ダウンロード時のファイル名を指定
68
+ header('Content-Disposition: attachment; filename="'.$file_name.'"');
69
+
70
+ // ファイル出力
71
+ readfile($file_name);
72
+ ?>
73
+ ```
74
+ ファイル「ほげほげ.docx」がダウンロードされ、 MS Wordで開くことができました。

3

変数設定値の追加

2019/11/26 01:45

投稿

Rebecca
Rebecca

スコア11

title CHANGED
File without changes
body CHANGED
@@ -19,6 +19,9 @@
19
19
  ### 以下、現状のソースです。
20
20
 
21
21
  ```cakePHP
22
+ $file_path = [サーバ上のディレクトリ/ファイル名];
23
+ $file_name = 'ほげほげ.docx';
24
+
22
25
  //ダウンロード処理
23
26
  $headers = [
24
27
  'Content-Type: application/octet-stream',
@@ -27,17 +30,21 @@
27
30
  ];
28
31
  $this->response->header($headers);
29
32
  $this->response->file($file_path, array('download' => true));
33
+ // $file_pathのファイル名をダウンロードファイル名にしてダウンロード
30
34
  $this->response->download($file_name);
31
35
  ```
32
36
 
33
37
  ### 以下、やってみたこと
34
38
  ```cakePHP
39
+ $file_path = [サーバ上のディレクトリ/ファイル名];
40
+ $file_name = 'ほげほげ.docx';
35
41
  $this->response->header('Content-Type: application/octet-stream');
36
42
  $content = 'attachment;';
37
43
  $content .= 'filename='.$file_name;
38
44
  $content .= ' filename*=UTF-8\'\''.rawurlencode($file_name);
39
45
  $this->response->header('Content-Disposition:', $content);
40
46
  $this->response->file($file_path);
47
+ // $file_pathのファイル名をダウンロードファイル名にしてダウンロード
41
48
  $this->response->download($file_name);
42
49
  ```
43
50
 

2

やってみたことの追加

2019/11/25 09:29

投稿

Rebecca
Rebecca

スコア11

title CHANGED
File without changes
body CHANGED
@@ -13,10 +13,10 @@
13
13
 
14
14
  ### 前提・実現したいこと
15
15
 
16
- SafariでもChromeと同様にファイルをダウンロードできでしょうか?
16
+ SafariでもChromeと同様にファイルをダウンロードできないでしょうか?
17
17
  ご教唆ください。
18
18
 
19
- ### 以下、やってみた現状のソースです。
19
+ ### 以下、現状のソースです。
20
20
 
21
21
  ```cakePHP
22
22
  //ダウンロード処理
@@ -30,6 +30,17 @@
30
30
  $this->response->download($file_name);
31
31
  ```
32
32
 
33
+ ### 以下、やってみたこと
34
+ ```cakePHP
35
+ $this->response->header('Content-Type: application/octet-stream');
36
+ $content = 'attachment;';
37
+ $content .= 'filename='.$file_name;
38
+ $content .= ' filename*=UTF-8\'\''.rawurlencode($file_name);
39
+ $this->response->header('Content-Disposition:', $content);
40
+ $this->response->file($file_path);
41
+ $this->response->download($file_name);
42
+ ```
43
+
33
44
  ### 補足情報(FW/ツールのバージョンなど)
34
45
 
35
46
  Mac OS

1

更新漏れによる再記載

2019/11/25 08:27

投稿

Rebecca
Rebecca

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,37 @@
1
+ 更新失敗していたようで、失礼しました。再度記載し直します。
2
+
1
3
  cakePHP 2.xで開発中です。
2
4
 
3
5
  アップロードしたファイルをダウンロードする機能があり、Safariでダウンロードすると、ファイル名最後に[.html]が追加されてしまいます。
6
+
4
7
  例えば、アップロードしたMS Wordファイル「xxx.docx」をダウンロードすると
5
8
  「xxx.docx.html」 となります。
6
9
 
7
- ファイル名から[.html]を削除するとMS Wordファイルとして開くことができます。
10
+ ファイル名から[.html]を削除するとMS Wordファイルとして開くことができます。
11
+ Chromeでは、正常に「xxx.docx」がダウンロードできます。
12
+
13
+
14
+ ### 前提・実現したいこと
15
+
16
+ SafariでもChromeと同様にファイルをダウンロードできるでしょうか?
17
+ ご教唆ください。
18
+
19
+ ### 以下、やってみた現状のソースです。
20
+
21
+ ```cakePHP
22
+ //ダウンロード処理
23
+ $headers = [
24
+ 'Content-Type: application/octet-stream',
25
+ 'Content-Disposition: attachment; filename="'.$file_name.'"',
26
+ 'Content-Length' => filesize($file_path)
27
+ ];
28
+ $this->response->header($headers);
29
+ $this->response->file($file_path, array('download' => true));
30
+ $this->response->download($file_name);
31
+ ```
32
+
33
+ ### 補足情報(FW/ツールのバージョンなど)
34
+
35
+ Mac OS
36
+ Vagrant + CentOS6.x
37
+ cakePHP2.x