回答編集履歴

1

回答追記

2017/01/05 07:22

投稿

Y.H.
Y.H.

スコア7914

test CHANGED
@@ -33,3 +33,73 @@
33
33
  @$zip->close();
34
34
 
35
35
  ```
36
+
37
+ ----
38
+
39
+ 追記:
40
+
41
+ 何らかの処理中にzipに格納するファイルが確定しかつ複数あるのであれば、先にまとめて存在チェックを行ってから処理する方法もあります。
42
+
43
+
44
+
45
+ ```PHP
46
+
47
+ // 処理対象のファイル
48
+
49
+ $img_save_path = '/tmp/';
50
+
51
+ $targetfiles = array(
52
+
53
+ array('filename' => 'file01.txt', 'savename' => 'file01.txt'),
54
+
55
+ array('filename' => 'file02.txt', 'savename' => 'file02.txt'),
56
+
57
+ array('filename' => 'file03.txt', 'savename' => 'file03.txt')
58
+
59
+ );
60
+
61
+
62
+
63
+ // 処理対象のファイルの存在チェックを行い、存在するもののみのリストを作成
64
+
65
+ $existsfiles = array();
66
+
67
+ foreach($targetfiles as $targetfile){
68
+
69
+ if (file_exists($img_save_path.$targetfile['filename'])) {
70
+
71
+ $existsfiles[] = $targetfile;
72
+
73
+ } else {
74
+
75
+ echo 'file not found : ' . $img_save_path.$targetfile['filename'] . PHP_EOL;
76
+
77
+ }
78
+
79
+ }
80
+
81
+
82
+
83
+ // 存在するもののみのリストにしたがってzipを作成
84
+
85
+ if (count($existsfiles) > 0) {
86
+
87
+ $zip->open($zip_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
88
+
89
+ foreach($existsfiles as $targetfile){
90
+
91
+ $zip->addFile($img_save_path.$targetfile['filename'], $targetfile['savename']);
92
+
93
+ }
94
+
95
+ $zip->close();
96
+
97
+ } else {
98
+
99
+ echo 'did not create a zip file.' . PHP_EOL;
100
+
101
+ }
102
+
103
+
104
+
105
+ ```