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

質問編集履歴

4

書式の改善

2019/08/23 01:00

投稿

sou_note
sou_note

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,86 +1,35 @@
1
1
  ### 実現したいこと
2
2
  Wordやexcelファイルを掲載しているページで、ファイルの直リンクを禁止したいです。
3
- ### 試したこと1
3
+
4
- ◯htaccessを使った直リンクの禁止
5
- (参考サト:http://mori.moripower.jp/eqwip/wordpress/1296.html
4
+ ●PHP、BASIC認証のかかったフォルダ配下のファルを、BASIC認証を経由していない人に閲覧させる(http://piyopiyocs.blog115.fc2.com/blog-entry-916.html)
5
+
6
+ download.php(ダウンロードページ)
6
7
  ```
7
- RewriteEngine On
8
- RewriteCond %{HTTP_REFERER} !^http://(ドメイン名).jp[NC]
9
- RewriteRule .(doc|xls|xlsx|rtf|docx|pdf)$ - [F]
10
- ```
11
- ### 結果(試したこと1)
12
- 直リンクからのダウンロードを禁止できましたが、(403エラー)
13
- 直リンクだけでなくサイトからもダウンロードができなくなりました。
14
- ### 試したこと2
15
- ◯htaccess、phpを使った直リンクの禁止
16
- (参考サイト:http://piyopiyocs.blog115.fc2.com/blog-entry-692.html)
17
- .htaccess
18
- ```
19
- <Files ~ ".(pdf|csv)$">
20
- deny from all
21
- </Files>
22
- ```
23
- ダウンロードページの1つ前のページ
24
- ```
25
8
  <?php
26
- //セッション開始
27
- session_start();
9
+ session_start();
28
-
10
+ if (isset($_SESSION['permission']) && $_SESSION[‘permission’] == 'true'){
11
+ header("Content-Type: application/pdf");
12
+ readfile(dirname(__FILE__) . "/test.pdf");
13
+ }
14
+ else{
29
- //セッション値設定
15
+ //不正アクスの場合の対処を記述
30
- $_SESSION{'flg'}= "ok";
16
+ }
31
17
  ?>
32
- ```
33
- ダウンロードページ
34
- ```
35
- <?php
36
- //セッション開始
37
- session_start();
38
18
 
39
- if(isset($_SESSION{'flg'}) && $_SESSION{'flg'} == "ok"){
19
+ <a href="img/download/sample.doc" target="_blank" rel="noopener noreferrer">サンプル</a>
40
- //PDF出力
41
- header("Content-Disposition: attachment; filename=test.doc");
42
- readfile(dirname(__FILE__) . "/test.doc");
43
- }else{
44
- print "不正遷移";
45
- }
46
- ?>
47
20
  ```
48
- ### 結果(試したこと2)
49
- 直リンクを禁止できましたが、
50
- サイトからアクセスする際にダウンロードページに遷移しようとすると、
51
- 設定したtest.docのダウンロードが開始されてしまい
52
- ファイルの一覧ページに遷移できませんでした。
53
21
 
54
- ### 追加分
22
+ sample.php
55
23
  ```
56
- download.php
24
+ <?php get_footer(); ?>
57
25
  <?php
58
- header('Content-Type: application/doc');
59
- header('Content-Length: '.filesize('sample.doc'));
60
- header('Content-Disposition: attachment; filename="sample.doc"');
61
- echo file_get_contents('sample.doc');
62
- exit;
63
-
64
- $key_1_content = array(
26
+ session_start();
65
- '1' => array(
66
- 'path' => './sample0.doc'
27
+ $_SESSION['permission']= 'true';
67
- ),
68
- '2' => array(
69
- 'path' => './sample1.doc'
70
- ),
71
- '3' => array(
72
- 'path' => './sample2.doc'
73
- )
74
- );
75
28
  ?>
76
- <?php
77
- // エラーを出力する
78
- ini_set('display_errors', "On");
79
- ?>
29
+ ```
80
30
 
81
- ダウンロードページ
82
- <a href="ドメイン名/download?key=myKey1">sample0.doc</a>
83
- ```
84
- ### 結果
31
+ ### 発生したエラー
85
- ダウロードページよりアセスをすると真っ白な画面が表示されてしまいます。
32
+ ンクをクリックすると
86
- その際のURLはhttp://ドメイン名.jp/download/?key=myKey1でした。
33
+ http://ドメイン名/sample/download/img/download/sample.doc
34
+ 上記のURLとなり、TOPページに遷移しダウンロードが開始されませんでした。
35
+ リンクの箇所を絶対パスで指定した場合も上記のリンクになりました。

3

書式の改善

2019/08/23 01:00

投稿

sou_note
sou_note

スコア7

title CHANGED
File without changes
body CHANGED
@@ -73,6 +73,10 @@
73
73
  )
74
74
  );
75
75
  ?>
76
+ <?php
77
+ // エラーを出力する
78
+ ini_set('display_errors', "On");
79
+ ?>
76
80
 
77
81
  ダウンロードページ
78
82
  <a href="ドメイン名/download?key=myKey1">sample0.doc</a>

2

誤字

2019/08/22 07:41

投稿

sou_note
sou_note

スコア7

title CHANGED
File without changes
body CHANGED
@@ -75,8 +75,8 @@
75
75
  ?>
76
76
 
77
77
  ダウンロードページ
78
- <a href="ドメイン名/download.php?key=myKey1">sample0.doc</a>
78
+ <a href="ドメイン名/download?key=myKey1">sample0.doc</a>
79
79
  ```
80
80
  ### 結果
81
- 1ファイル分だけの設定であれば正常にダウンロードできたのですが、
82
- 複数ページの設定の場合だ見つからいとのことでトップページに遷移してしまいました
81
+ ダウンロードページよりアクセスをする真っ白画面が表示されてしまいま
82
+ その際のURLはhttp://ドメイン名.jp/download/?key=myKey1でした。

1

書式の改善

2019/08/22 05:18

投稿

sou_note
sou_note

スコア7

title CHANGED
File without changes
body CHANGED
@@ -3,23 +3,25 @@
3
3
  ### 試したこと1
4
4
  ◯htaccessを使った直リンクの禁止
5
5
  (参考サイト:http://mori.moripower.jp/eqwip/wordpress/1296.html)
6
- ### コード(試したこと1)
6
+ ```
7
7
  RewriteEngine On
8
8
  RewriteCond %{HTTP_REFERER} !^http://(ドメイン名).jp[NC]
9
9
  RewriteRule .(doc|xls|xlsx|rtf|docx|pdf)$ - [F]
10
+ ```
10
11
  ### 結果(試したこと1)
11
12
  直リンクからのダウンロードを禁止できましたが、(403エラー)
12
13
  直リンクだけでなくサイトからもダウンロードができなくなりました。
13
14
  ### 試したこと2
14
15
  ◯htaccess、phpを使った直リンクの禁止
15
16
  (参考サイト:http://piyopiyocs.blog115.fc2.com/blog-entry-692.html)
16
- ### コード(試したこと2)
17
17
  .htaccess
18
+ ```
18
19
  <Files ~ ".(pdf|csv)$">
19
20
  deny from all
20
21
  </Files>
21
-
22
+ ```
22
23
  ダウンロードページの1つ前のページ
24
+ ```
23
25
  <?php
24
26
  //セッション開始
25
27
  session_start();
@@ -27,8 +29,9 @@
27
29
  //セッション値設定
28
30
  $_SESSION{'flg'}= "ok";
29
31
  ?>
30
-
32
+ ```
31
33
  ダウンロードページ
34
+ ```
32
35
  <?php
33
36
  //セッション開始
34
37
  session_start();
@@ -41,8 +44,39 @@
41
44
  print "不正遷移";
42
45
  }
43
46
  ?>
47
+ ```
44
48
  ### 結果(試したこと2)
45
49
  直リンクを禁止できましたが、
46
50
  サイトからアクセスする際にダウンロードページに遷移しようとすると、
47
51
  設定したtest.docのダウンロードが開始されてしまい
48
- ファイルの一覧ページに遷移できませんでした。
52
+ ファイルの一覧ページに遷移できませんでした。
53
+
54
+ ### 追加分
55
+ ```
56
+ download.php
57
+ <?php
58
+ header('Content-Type: application/doc');
59
+ header('Content-Length: '.filesize('sample.doc'));
60
+ header('Content-Disposition: attachment; filename="sample.doc"');
61
+ echo file_get_contents('sample.doc');
62
+ exit;
63
+
64
+ $key_1_content = array(
65
+ '1' => array(
66
+ 'path' => './sample0.doc'
67
+ ),
68
+ '2' => array(
69
+ 'path' => './sample1.doc'
70
+ ),
71
+ '3' => array(
72
+ 'path' => './sample2.doc'
73
+ )
74
+ );
75
+ ?>
76
+
77
+ ダウンロードページ
78
+ <a href="ドメイン名/download.php?key=myKey1">sample0.doc</a>
79
+ ```
80
+ ### 結果
81
+ 1ファイル分だけの設定であれば正常にダウンロードできたのですが、
82
+ 複数ページの設定の場合だと見つからないとのことでトップページに遷移してしまいました。