質問編集履歴

12

コード整形

2021/05/06 15:31

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -158,6 +158,8 @@
158
158
 
159
159
  <button type="submit" name="add" class="section1-content-button" onsubmit="return check_blank(this)" >ダウンロード</button >
160
160
 
161
+ <!-- 省略 -->
162
+
161
163
  ```
162
164
 
163
165
 

11

タイポ修正

2021/05/06 15:31

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -156,7 +156,7 @@
156
156
 
157
157
  </div></br></br>
158
158
 
159
- <button type="submit" name="createDressUp" class="section1-content-button" onsubmit="return check_blank(this)" >ダウンロード</button >
159
+ <button type="submit" name="add" class="section1-content-button" onsubmit="return check_blank(this)" >ダウンロード</button >
160
160
 
161
161
  ```
162
162
 

10

解決策を記載。

2021/05/06 15:28

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -646,7 +646,7 @@
646
646
 
647
647
 
648
648
 
649
- ### 試したこと最新
649
+ ### 試したこと最新(CHERRYさんからいただいた方針です。こちらで期待動作となりました。)
650
650
 
651
651
  <?php の次行からダウンロードの処理を記載して、テーマの処理が始まる前にダウンロード処理するコードを記載することを
652
652
 

9

試したことを追加

2021/05/06 14:32

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -528,28 +528,6 @@
528
528
 
529
529
  <!-- 文字制限により中略 -->
530
530
 
531
- /*
532
-
533
- * If you're looking at a src version of this file, you'll see an "include"
534
-
535
- * statement below. This is used by the `npm run build` process to directly
536
-
537
- * include a minified version of wp-emoji-loader.js, instead of using the
538
-
539
- * readfile() method from above.
540
-
541
- *
542
-
543
- * If you're looking at a build version of this file, you'll see a string of
544
-
545
- * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
546
-
547
- * and edit wp-emoji-loader.js directly.
548
-
549
- */
550
-
551
- ?>
552
-
553
531
  <script<?php echo $type_attr; ?>>
554
532
 
555
533
 
@@ -668,6 +646,182 @@
668
646
 
669
647
 
670
648
 
649
+ ### 試したこと最新
650
+
651
+ <?php の次行からダウンロードの処理を記載して、テーマの処理が始まる前にダウンロード処理するコードを記載することを
652
+
653
+ 試しました。以下コードです。
654
+
655
+ (問題をシンプルにするために header関数を使わないようにしました。)
656
+
657
+
658
+
659
+ ``` PHP
660
+
661
+ <!-- page.php -->
662
+
663
+
664
+
665
+ <?php
666
+
667
+ define('WP_DEBUG', false);
668
+
669
+
670
+
671
+ function download($pPath) {
672
+
673
+ //-- Content-Type
674
+
675
+ header('Content-Type: application/zip');
676
+
677
+
678
+
679
+ //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
680
+
681
+ header('X-Content-Type-Options: nosniff');
682
+
683
+
684
+
685
+ //-- ダウンロードファイルのサイズ
686
+
687
+ header('Content-Length: ' . filesize($pPath));
688
+
689
+
690
+
691
+ //-- ダウンロード時のファイル名
692
+
693
+ header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
694
+
695
+
696
+
697
+ //-- keep-aliveを無効にする
698
+
699
+ header('Connection: close');
700
+
701
+
702
+
703
+ //-- readfile()の前に出力バッファリングを無効化する
704
+
705
+ while (ob_get_level()) { ob_end_clean(); }
706
+
707
+
708
+
709
+ //-- 出力
710
+
711
+ readfile($pPath);
712
+
713
+
714
+
715
+ // 作成したzipは削除しておく
716
+
717
+ unlink($pPath);
718
+
719
+
720
+
721
+ //-- 最後に終了させるのを忘れない
722
+
723
+ exit;
724
+
725
+ }
726
+
727
+
728
+
729
+
730
+
731
+ if(isset($_POST['add'])) {
732
+
733
+
734
+
735
+ // zip 作成処理
736
+
737
+
738
+
739
+ // 出力処理 ※”$zip_tmp_path.'/'.$zip_name” は zipファイルのパス
740
+
741
+ download($zip_tmp_path.'/'.$zip_name);
742
+
743
+
744
+
745
+ }
746
+
747
+ ?>
748
+
749
+
750
+
751
+
752
+
753
+ <head>
754
+
755
+ <meta charset="utf-8">
756
+
757
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
758
+
759
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"/>
760
+
761
+ <meta name="referrer" content="no-referrer-when-downgrade"/>
762
+
763
+ </head>
764
+
765
+
766
+
767
+ <?php
768
+
769
+ if(have_posts()){
770
+
771
+ while(have_posts()){
772
+
773
+ the_post();
774
+
775
+ the_content();
776
+
777
+ }
778
+
779
+ }
780
+
781
+ ?>
782
+
783
+ ```
784
+
785
+
786
+
787
+ ``` HTML
788
+
789
+ <!-- 記事内容 -->
790
+
791
+
792
+
793
+ <!-- 省略 -->
794
+
795
+
796
+
797
+ <form method="POST" action="" enctype="multipart/form-data">
798
+
799
+ <div class="file_button">
800
+
801
+ <input class="input-file" type="file" name="upimg" accept="image/png">
802
+
803
+ </div></br></br>
804
+
805
+ <button type="submit" name="add" class="section1-content-button" onsubmit="check_blank(this);" >ダウンロード</button >
806
+
807
+ </form>
808
+
809
+
810
+
811
+ <!-- 省略 -->
812
+
813
+
814
+
815
+
816
+
817
+ ```
818
+
819
+
820
+
821
+ 結果、期待通りに download 処理を行うことができました。
822
+
823
+
824
+
671
825
 
672
826
 
673
827
  ### 補足情報(FW/ツールのバージョンなど)

8

b_cleanで出力バッファを意図的にクリアしてみたときの挙動追記

2021/05/06 14:28

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -560,12 +560,6 @@
560
560
 
561
561
  window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
562
562
 
563
- !function(e,a,t){var n,r,o,i=a.createElement("canvas"),p=i.getContext&&i.getContext("2d");function s(e,t){var a=String.fromCharCode;p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,e),0,0);e=i.toDataURL();return p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,t),0,0),e===i.toDataURL()}function c(e){var t=a.createElement("script");t.src=e,t.defer=t.type="text/javascript",a.getElementsByTagName("head")[0].appendChild(t)}for(o=Array("flag","emoji"),t.supports={everything:!0,everythingExceptFlag:!0},r=0;r<o.length;r++)t.supports[o[r]]=function(e){if(!p||!p.fillText)return!1;switch(p.textBaseline="top",p.font="600 32px Arial",e){case"flag":return s([127987,65039,8205,9895,65039],[127987,65039,8203,9895,65039])?!1:!s([55356,56826,55356,56819],[55356,56826,8203,55356,56819])&&!s([55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447],[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447]);case"emoji":return!s([55357,56424,8205,55356,57212],[55357,56424,8203,55356,57212])}return!1}(o[r]),t.supports.everything=t.supports.everything&&t.supports[o[r]],"flag"!==o[r]&&(t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&t.supports[o[r]]);t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&!t.supports.flag,t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.everything||(n=function(){t.readyCallback()},a.addEventListener?(a.addEventListener("DOMContentLoaded",n,!1),e.addEventListener("load",n,!1)):(e.attachEvent("onload",n),a.attachEvent("onreadystatechange",function(){"complete"===a.readyState&&t.readyCallback()})),(n=t.source||{}).concatemoji?c(n.concatemoji):n.wpemoji&&n.twemoji&&(c(n.twemoji),c(n.wpemoji)))}(window,document,window._wpemojiSettings);
564
-
565
- </script>
566
-
567
- <?php
568
-
569
563
  }
570
564
 
571
565
  }
@@ -574,6 +568,108 @@
574
568
 
575
569
 
576
570
 
571
+ ■ header関数使う前に、ob_cleanで出力バッファを意図的にクリアして、出力の終わりにob_flushする
572
+
573
+ ご助言いただき、ob_●● を試してみました。
574
+
575
+
576
+
577
+ ``` PHP
578
+
579
+ function download($pPath) {
580
+
581
+ if (!ob_clean()) {
582
+
583
+ echo <<<EOM
584
+
585
+ <script>alert('ob_cleanの失敗');</script>
586
+
587
+ EOM;
588
+
589
+ exit;
590
+
591
+ }
592
+
593
+
594
+
595
+ //-- Content-Type
596
+
597
+ header('Content-Type: application/zip');
598
+
599
+
600
+
601
+ //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
602
+
603
+ header('X-Content-Type-Options: nosniff');
604
+
605
+
606
+
607
+ //-- ダウンロードファイルのサイズ
608
+
609
+ header('Content-Length: ' . filesize($pPath));
610
+
611
+
612
+
613
+ //-- ダウンロード時のファイル名
614
+
615
+ header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
616
+
617
+
618
+
619
+ //-- keep-aliveを無効にする
620
+
621
+ header('Connection: close');
622
+
623
+
624
+
625
+ if (!ob_flush()) {
626
+
627
+ echo <<<EOM
628
+
629
+ <script>alert('ob_flushの失敗');</script>
630
+
631
+ EOM;
632
+
633
+ exit;
634
+
635
+ }
636
+
637
+
638
+
639
+ //-- 出力
640
+
641
+ readfile($pPath);
642
+
643
+
644
+
645
+ // 作成したzipは削除しておく
646
+
647
+ unlink($pPath);
648
+
649
+
650
+
651
+ //-- 最後に終了させるのを忘れない
652
+
653
+ exit;
654
+
655
+ }
656
+
657
+ ```
658
+
659
+
660
+
661
+ 上記のように、header 処理の前に、出力バッファをクリアしてからフラッシュして
662
+
663
+ readfileしましたが、結果は不具合結果と同じでした。
664
+
665
+ ob_cleanした後に、headers_sent()で送信済みかどうか確認してみると既に送信済み
666
+
667
+ 判定となっているため、同不具合が発生していると思われます。
668
+
669
+
670
+
671
+
672
+
577
673
  ### 補足情報(FW/ツールのバージョンなど)
578
674
 
579
675
  OS:Windows 10 pro(64bit)

7

質問内容について、ヘッダ送信済みとなってしまう契機について追記いたしました。

2021/05/06 12:11

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,28 @@
28
28
 
29
29
 
30
30
 
31
+ 補足:
32
+
33
+ itagagakiさんのご助言があって、
34
+
35
+ headers_sent関数で確認したところ既にヘッダ送信済みのため、
36
+
37
+ headerのcontent-typeの設定が上手くいっていないことが
38
+
39
+ 分かりました。
40
+
41
+ しかし、該当の既に送信を行ったモジュールはテーマ共通のPHPファイルであったため、
42
+
43
+ なぜPOST後のレスポンス送信時のエラーで差異が出るのかが不明です。
44
+
45
+ (既存テーマの(Cocoon)ではうまくいっています。)
46
+
47
+ このヘッダ送信済みとなってしまう "契機" の観点で 問題のありそうな部分があれば
48
+
49
+ ご助言いただきたいです…。
50
+
51
+
52
+
31
53
  ## 前提
32
54
 
33
55
 

6

headerが送信済みかどうかのチェックの検証結果を追記しました。

2021/05/05 10:47

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -358,6 +358,198 @@
358
358
 
359
359
 
360
360
 
361
+ ■ header関数を呼ぶ前にheaders_sent関数で確認してみました。
362
+
363
+
364
+
365
+ ご助言いただき、headers_sent関数で既にヘッダが送信されているか確認しました。
366
+
367
+ 以下コードです。
368
+
369
+
370
+
371
+ ``` PHP
372
+
373
+ function download($pPath) {
374
+
375
+
376
+
377
+ if (!headers_sent($filename, $linenum)) {
378
+
379
+
380
+
381
+ //-- Content-Type
382
+
383
+ header('Content-Type: application/zip');
384
+
385
+
386
+
387
+ //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
388
+
389
+ header('X-Content-Type-Options: nosniff');
390
+
391
+
392
+
393
+ //-- ダウンロードファイルのサイズ
394
+
395
+ header('Content-Length: ' . filesize($pPath));
396
+
397
+
398
+
399
+ //-- ダウンロード時のファイル名
400
+
401
+ header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
402
+
403
+
404
+
405
+ //-- keep-aliveを無効にする
406
+
407
+ header('Connection: close');
408
+
409
+
410
+
411
+ //-- readfile()の前に出力バッファリングを無効化する ※詳細は後述
412
+
413
+ while (ob_get_level()) { ob_end_clean(); }
414
+
415
+
416
+
417
+ //-- 出力
418
+
419
+ readfile($pPath);
420
+
421
+
422
+
423
+ // 作成したzipは削除しておく
424
+
425
+ unlink($pPath);
426
+
427
+
428
+
429
+ //-- 最後に終了させるのを忘れない
430
+
431
+ exit;
432
+
433
+ }
434
+
435
+ else {
436
+
437
+ echo "$filename の $linenum 行目でヘッダがすでに送信されています。\n" .
438
+
439
+ "リダイレクトできません。代わりにこの <a " .
440
+
441
+ "href=\"http://www.example.com\">リンク</a> をクリックしてください。\n";
442
+
443
+ exit;
444
+
445
+ }
446
+
447
+ }
448
+
449
+ ```
450
+
451
+
452
+
453
+ 確認したところ、同じPHPコードで Wordpressのテーマ Cocoon では既にヘッダ送信済み判定ではなかったのですが、
454
+
455
+ 自作テーマでは既にヘッダ送信済み判定となりました。
456
+
457
+
458
+
459
+ 実行結果:
460
+
461
+ /home/c1221591/public_html/[質問者の取得しているドメイン].com/wp-includes/formatting.php の 5740 行目で
462
+
463
+ ヘッダがすでに送信されています。 リダイレクトできません。代わりにこの リンク をクリックしてください。
464
+
465
+
466
+
467
+ 該当の送信済みとなったformatting.phpのコードは以下の箇所でした。
468
+
469
+
470
+
471
+ ※注釈つけています。
472
+
473
+  該当箇所:
474
+
475
+    <!-- ↓↓↓ ここが 5740行目 ↓↓↓ -->
476
+
477
+   window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
478
+
479
+
480
+
481
+ 参考文献:
482
+
483
+ https://developer.wordpress.org/reference/functions/print_emoji_detection_script/
484
+
485
+
486
+
487
+ ``` PHP
488
+
489
+ /**
490
+
491
+ * Prints inline Emoji detection script.
492
+
493
+ *
494
+
495
+ * @ignore
496
+
497
+ * @since 4.6.0
498
+
499
+ * @access private
500
+
501
+ */
502
+
503
+ function _print_emoji_detection_script() {
504
+
505
+ $settings = array(
506
+
507
+ <!-- 文字制限により中略 -->
508
+
509
+ /*
510
+
511
+ * If you're looking at a src version of this file, you'll see an "include"
512
+
513
+ * statement below. This is used by the `npm run build` process to directly
514
+
515
+ * include a minified version of wp-emoji-loader.js, instead of using the
516
+
517
+ * readfile() method from above.
518
+
519
+ *
520
+
521
+ * If you're looking at a build version of this file, you'll see a string of
522
+
523
+ * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG
524
+
525
+ * and edit wp-emoji-loader.js directly.
526
+
527
+ */
528
+
529
+ ?>
530
+
531
+ <script<?php echo $type_attr; ?>>
532
+
533
+
534
+
535
+
536
+
537
+ <!-- ↓↓↓ ここが 5740行目 ↓↓↓ -->
538
+
539
+ window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>;
540
+
541
+ !function(e,a,t){var n,r,o,i=a.createElement("canvas"),p=i.getContext&&i.getContext("2d");function s(e,t){var a=String.fromCharCode;p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,e),0,0);e=i.toDataURL();return p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,t),0,0),e===i.toDataURL()}function c(e){var t=a.createElement("script");t.src=e,t.defer=t.type="text/javascript",a.getElementsByTagName("head")[0].appendChild(t)}for(o=Array("flag","emoji"),t.supports={everything:!0,everythingExceptFlag:!0},r=0;r<o.length;r++)t.supports[o[r]]=function(e){if(!p||!p.fillText)return!1;switch(p.textBaseline="top",p.font="600 32px Arial",e){case"flag":return s([127987,65039,8205,9895,65039],[127987,65039,8203,9895,65039])?!1:!s([55356,56826,55356,56819],[55356,56826,8203,55356,56819])&&!s([55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447],[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447]);case"emoji":return!s([55357,56424,8205,55356,57212],[55357,56424,8203,55356,57212])}return!1}(o[r]),t.supports.everything=t.supports.everything&&t.supports[o[r]],"flag"!==o[r]&&(t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&t.supports[o[r]]);t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&!t.supports.flag,t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.everything||(n=function(){t.readyCallback()},a.addEventListener?(a.addEventListener("DOMContentLoaded",n,!1),e.addEventListener("load",n,!1)):(e.attachEvent("onload",n),a.attachEvent("onreadystatechange",function(){"complete"===a.readyState&&t.readyCallback()})),(n=t.source||{}).concatemoji?c(n.concatemoji):n.wpemoji&&n.twemoji&&(c(n.twemoji),c(n.wpemoji)))}(window,document,window._wpemojiSettings);
542
+
543
+ </script>
544
+
545
+ <?php
546
+
547
+ }
548
+
549
+ }
550
+
551
+ ```
552
+
361
553
 
362
554
 
363
555
  ### 補足情報(FW/ツールのバージョンなど)

5

現象の補足と、前提の補足を行いました。

2021/05/05 10:27

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -42,6 +42,12 @@
42
42
 
43
43
 
44
44
 
45
+       現象が起きているページは、
46
+
47
+       WordPress の 固定ページ(page.php)です。
48
+
49
+
50
+
45
51
   クライアントのダウンロード方法は一般的でブラウザを使ってページにアクセスし、
46
52
 
47
53
   ボタン押下でエクスプローラーから保存する経由になります。
@@ -68,6 +74,12 @@
68
74
 
69
75
      (以下の画像参照です。)
70
76
 
77
+      文字列が表示されるケースでは、ページ遷移が一度走って、
78
+
79
+      遷移後のページで以下のようなページが表示されます。
80
+
81
+      (URLは遷移前と遷移後で同じ)
82
+
71
83
 
72
84
 
73
85
  ![イメージ説明](1a8b961fa9985e18848bfc3b4a366ba3.png)

4

質問文整形(話の内容は変更なし)

2021/05/05 10:09

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -246,7 +246,9 @@
246
246
 
247
247
 
248
248
 
249
-  調査を行って現象として、以下記事に近いと感じたため文字コードの改善を試みました。
249
+  調査を行って現象として、以下記事に近いと感じたため文字コードの改善を試みました。
250
+
251
+
250
252
 
251
253
   「【PHP】headerでContent-Typeを指定したのに効かない場合の対処法」
252
254
 
@@ -262,6 +264,10 @@
262
264
 
263
265
 
264
266
 
267
+ ■ type を 無条件に zip 指定のコードで現象起こるかどうか
268
+
269
+
270
+
265
271
   ご助言いただき、PHPコードにて無条件で
266
272
 
267
273
   header('Content-Type: application/zip')

3

無条件にtype指定してダウンロードすることを試した結果を記載しました。

2021/05/05 09:55

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -262,6 +262,84 @@
262
262
 
263
263
 
264
264
 
265
+  ご助言いただき、PHPコードにて無条件で
266
+
267
+  header('Content-Type: application/zip')
268
+
269
+  を指定してダウンロード処理を実行してみました。
270
+
271
+
272
+
273
+  結果は変わりませんでした。以下試したダウンロード処理部のコードです。
274
+
275
+  (引数に正しくzipファイルのパスを渡しています。)
276
+
277
+
278
+
279
+ ``` PHP
280
+
281
+ function download($pPath) {
282
+
283
+
284
+
285
+ //-- Content-Type
286
+
287
+ header('Content-Type: application/zip');
288
+
289
+
290
+
291
+ //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
292
+
293
+ header('X-Content-Type-Options: nosniff');
294
+
295
+
296
+
297
+ //-- ダウンロードファイルのサイズ
298
+
299
+ header('Content-Length: ' . filesize($pPath));
300
+
301
+
302
+
303
+ //-- ダウンロード時のファイル名
304
+
305
+ header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
306
+
307
+
308
+
309
+ //-- keep-aliveを無効にする
310
+
311
+ header('Connection: close');
312
+
313
+
314
+
315
+ //-- readfile()の前に出力バッファリングを無効化する ※詳細は後述
316
+
317
+ while (ob_get_level()) { ob_end_clean(); }
318
+
319
+
320
+
321
+ //-- 出力
322
+
323
+ readfile($pPath);
324
+
325
+
326
+
327
+ // 作成したzipは削除しておく
328
+
329
+ unlink($pPath);
330
+
331
+
332
+
333
+ //-- 最後に終了させるのを忘れない
334
+
335
+ exit;
336
+
337
+ }
338
+
339
+ ```
340
+
341
+
342
+
265
343
 
266
344
 
267
345
  ### 補足情報(FW/ツールのバージョンなど)

2

PHPコードのコメントを分かりやすいように修正

2021/05/05 09:52

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -214,6 +214,12 @@
214
214
 
215
215
  if(isset($_POST['add'])) {
216
216
 
217
+
218
+
219
+ // !!! 中略
220
+
221
+
222
+
217
223
  // $zip_tmp_path、$zip_nameはそれぞれサーバ上のZIPファイルの場所と名前
218
224
 
219
225
  $res = $zip->open($zip_tmp_path.'/'.$zip_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
@@ -222,6 +228,8 @@
222
228
 
223
229
     // !!! 中略(ここからファイルダウンロードさせるための処理実行) !!!
224
230
 
231
+
232
+
225
233
  // $pMimeTypeの引数は未指定で実行
226
234
 
227
235
  download($zip_tmp_path.'/'.$zip_name);

1

pMimeType がどこから来たのかについて、分かるようにPHPコード修正しました。

2021/05/05 09:38

投稿

isMiss
isMiss

スコア1

test CHANGED
File without changes
test CHANGED
@@ -132,87 +132,101 @@
132
132
 
133
133
  // !!! OPEN処理とZIPファイルダウンロード処理部以外は、長くなるので割愛させていただきます。 !!!
134
134
 
135
-
136
-
137
- // $zip_tmp_path、$zip_nameはそれぞれサーバ上のZIPファイルの場所と名前
138
-
139
- $res = $zip->open($zip_tmp_path.'/'.$zip_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
140
-
141
-
142
-
143
- // !!! 中略(ここからZIPファイルダウンロードさせるための処理開始) !!!
135
+ // ファイルダウンロードさせるための処理定義
136
+
144
-
137
+ function download($pPath, $pMimeType = null) {
145
-
146
-
138
+
147
- //-- Content-Typeとして送信するMIMEタイプ
139
+ //-- Content-Typeとして送信するMIMEタイプ
148
-
140
+
149
- $mimeType = (isset($pMimeType)) ? $pMimeType
141
+ $mimeType = (isset($pMimeType)) ? $pMimeType
150
-
142
+
151
- : (new finfo(FILEINFO_MIME_TYPE))->file($pPath);
143
+ : (new finfo(FILEINFO_MIME_TYPE))->file($pPath);
152
-
153
-
154
-
144
+
145
+
146
+
155
- //-- 適切なMIMEタイプが得られない時は、未知のファイルを示すapplication/octet-streamとする
147
+ //-- 適切なMIMEタイプが得られない時は、未知のファイルを示すapplication/octet-streamとする
156
-
148
+
157
- if (!preg_match('/\A\S+?/\S+/', $mimeType)) {
149
+ if (!preg_match('/\A\S+?/\S+/', $mimeType)) {
158
-
150
+
159
- $mimeType = 'application/octet-stream';
151
+ $mimeType = 'application/octet-stream';
152
+
153
+ }
154
+
155
+
156
+
157
+ //-- Content-Type
158
+
159
+ header('Content-Type: ' . $mimeType);
160
+
161
+
162
+
163
+ //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
164
+
165
+ header('X-Content-Type-Options: nosniff');
166
+
167
+
168
+
169
+ //-- ダウンロードファイルのサイズ
170
+
171
+ header('Content-Length: ' . filesize($pPath));
172
+
173
+
174
+
175
+ //-- ダウンロード時のファイル名
176
+
177
+ header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
178
+
179
+
180
+
181
+ //-- keep-aliveを無効にする
182
+
183
+ header('Connection: close');
184
+
185
+
186
+
187
+ //-- readfile()の前に出力バッファリングを無効化する
188
+
189
+ while (ob_get_level()) { ob_end_clean(); }
190
+
191
+
192
+
193
+ //-- 出力
194
+
195
+ readfile($pPath);
196
+
197
+
198
+
199
+ // 作成したzipは削除しておく
200
+
201
+ unlink($pPath);
202
+
203
+
204
+
205
+ //-- 最後に終了させる
206
+
207
+ exit;
160
208
 
161
209
  }
162
210
 
163
211
 
164
212
 
165
- //-- Content-Type
166
-
167
- header('Content-Type: ' . $mimeType);
168
-
169
-
170
-
171
- //-- ウェブブラウザが独自にMIMEタイプを判断する処理を抑止する
172
-
173
- header('X-Content-Type-Options: nosniff');
174
-
175
-
176
-
177
- //-- ダウンロードファイルのサイズ
178
-
179
- header('Content-Length: ' . filesize($pPath));
180
-
181
-
182
-
183
- //-- ダウンロード時のファイル名
184
-
185
- header('Content-Disposition: attachment; filename="' . basename($pPath) . '"');
186
-
187
-
188
-
189
- //-- keep-aliveを無効にする
190
-
191
- header('Connection: close');
192
-
193
-
194
-
195
- //-- readfile()の前に出力バッファリングを無効化する
196
-
197
- while (ob_get_level()) { ob_end_clean(); }
198
-
199
-
200
-
201
- //-- 出力
202
-
203
- readfile($pPath);
204
-
205
-
206
-
207
- // 作成しzipは削除しておく
213
+   // POSTされときの処理開始
208
-
214
+
209
- unlink($pPath);
215
+ if(isset($_POST['add'])) {
216
+
210
-
217
+ // $zip_tmp_path、$zip_nameはそれぞれサーバ上のZIPファイルの場所と名前
218
+
211
-
219
+ $res = $zip->open($zip_tmp_path.'/'.$zip_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
220
+
221
+
222
+
212
-
223
+    // !!! 中略(ここからファイルダウンロードさせるための処理実行) !!!
224
+
213
- //-- 最後に終了させる
225
+ // $pMimeTypeの引数は未指定で実行
226
+
214
-
227
+ download($zip_tmp_path.'/'.$zip_name);
228
+
215
- exit;
229
+ }
216
230
 
217
231
  ```
218
232