为什么我上传什么文件 都说扩展名不对文件类型不对

为什么我上传什么文件 都说扩展名不对文件类型不对

封装代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
 
class UploadFile
{
 
    /**
     *
     */
    const UPLOAD_ERROR = [
        UPLOAD_ERR_INI_SIZE => '文件大小超出了php.ini当中的upload_max_filesize的值',
        UPLOAD_ERR_FORM_SIZE => '文件大小超出了MAX_FILE_SIZE的值',
        UPLOAD_ERR_PARTIAL => '文件只有部分被上传',
        UPLOAD_ERR_NO_FILE => '没有文件被上传',
        UPLOAD_ERR_NO_TMP_DIR => '找不到临时目录',
        UPLOAD_ERR_CANT_WRITE => '写入磁盘失败',
        UPLOAD_ERR_EXTENSION => '文件上传被扩展阻止',
    ];
 
    /**
     * @var
     */
    protected $field_name//受保护的,只有本类或子类或父类中可以访问  文件名
 
    /**
     * @var string
     */
    protected $destination_dir;  //目录路径
 
    /**
     * @var array
     */
    protected $allow_mime;
 
    /**
     * @var array
     */
    protected $allow_ext;
 
    /**
     * @var
     */
    protected $file_org_name;
 
    /**
     * @var
     */
    protected $file_type;
 
    /**
     * @var
     */
    protected $file_tmp_name;
 
    /**
     * @var
     */
    protected $file_error;
 
    /**
     * @var
     */
    protected $file_size;
 
    /**
     * @var array
     */
    protected $errors;
 
    /**
     * @var
     */
    protected $extension;
 
    /**
     * @var
     */
    protected $file_new_name;
 
    /**
     * @var float|int
     */
    protected $allow_size;
 
    /**
     * UploadFile constructor.
     * @param $keyName
     * @param string $destinationDir
     * @param array $allowMime
     * @param array $allowExt
     * @param float|int $allowSize
     */
    public function __construct($keyName$destinationDir './uploads'$allowMime = ['image/jpeg''image/gif'], $allowExt = ['gif''jpeg'], $allowSize = 2*1024*1024) //如果成功,则该函数返回一个对象。如果失败,则返回 false。
    {
        $this->field_name = $keyName;
        $this->destination_dir = $destinationDir;
        $this->allow_mime = $allowMime;
        $this->allow_ext = $allowExt;
        $this->allow_size = $allowSize;
    }
 
    /**
     * @param $destinationDir
     */
    public function setDestinationDir($destinationDir)
    {
        $this->destination_dir = $destinationDir;
    }
 
    /**
     * @param $allowMime
     */
    public function setAllowMime($allowMime)
    {
        $this->allow_mime = $allowMime;
    }
 
    /**
     * @param $allowExt
     */
    public function setAllowExt($allowExt)
    {
        $this->allow_ext = $allowExt;
    }
 
    /**
     * @param $allowSize
     */
    public function setAllowSize($allowSize)
    {
        $this->allow_size = $allowSize;
    }
 
    /**
     * @return bool
     */
    public function upload()
    {
        // 判断是否为多文件上传
        $files = [];
        if (is_array($_FILES[$this->field_name]['name'])) {
            foreach($_FILES[$this->field_name]['name'as $k => $v) {
                $files[$k]['name'] = $v;
                $files[$k]['type'] = $_FILES[$this->field_name]['type'][$k];
                $files[$k]['tmp_name'] = $_FILES[$this->field_name]['tmp_name'][$k];
                $files[$k]['error'] = $_FILES[$this->field_name]['error'][$k];
                $files[$k]['size'] = $_FILES[$this->field_name]['size'][$k];
            }
        else {
            $files[] = $_FILES[$this->field_name];
        }
 
        foreach($files as $key => $file) {
            // 接收$_FILES参数
            $this->setFileInfo($key$file);
 
            // 检查错误
            $this->checkError($key);
 
            // 检查MIME类型
            $this->checkMime($key);
 
            // 检查扩展名
            $this->checkExt($key);
 
            // 检查文件大小
            $this->checkSize($key);
 
            // 生成新的文件名称
            $this->generateNewName($key);
 
            if (count((array)$this->getError($key)) > 0) {
                continue;
            }
            // 移动文件
            $this->moveFile($key);
        }
        if (count((array)$this->errors) > 0) {
            return false;
        }
        return true;
    }
 
    /**
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }
 
    /**
     * @param $key
     * @return mixed
     */
    protected function getError($key)
    {
        return $this->errors[$key];
    }
 
    /**
     *
     */
    protected function setFileInfo($key$file)  //用来设定 接收到$_FILES的参数
    {
        // $_FILES  name type temp_name error size
        $this->file_org_name[$key] = $file['name'];
        $this->file_type[$key] = $file['type'];
        $this->file_tmp_name[$key] = $file['tmp_name'];
        $this->file_error[$key] = $file['error'];
        $this->file_size[$key] = $file['size'];
    }
 
 
    /**
     * @param $key
     * @param $error
     */
    protected function setError($key$error)
    {
        $this->errors[$key][] = $error;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkError($key)
    {
        if ($this->file_error > UPLOAD_ERR_OK) {
            switch($this->file_error) {
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                case UPLOAD_ERR_PARTIAL:
                case UPLOAD_ERR_NO_FILE:
                case UPLOAD_ERR_NO_TMP_DIR:
                case UPLOAD_ERR_CANT_WRITE:
                case UPLOAD_ERR_EXTENSION:
                    $this->setError($key, self::UPLOAD_ERROR[$this->file_error]);  //内部访问常量用self::
                    return false;
            }
        }
        return true;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkMime($key)
    {
        if (!in_array($this->file_type[$key], $this->allow_mime)) {
            $this->setError($key'文件类型' $this->file_type[$key] . '不被允许!');
            return false;
        }
        return true;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkExt($key)
    {
        $this->extension[$key] = pathinfo($this->file_org_name[$key], PATHINFO_EXTENSION);
        if (!in_array($this->extension[$key], $this->allow_ext)) {
            $this->setError($key'文件扩展名' $this->extension[$key] . '不被允许!');
            return false;
        }
        return true;
    }
 
    /**
     * @return bool
     */
    protected function checkSize($key)
    {
        if ($this->file_size[$key] > $this->allow_size) {
            $this->setError($key'文件大小' $this->file_size[$key] . '超出了限定大小' $this->allow_size);
            return false;
        }
        return true;
    }
 
 
    /**
     * @param $key
     */
    protected function generateNewName($key)  //生成新的文件名
    {
        $this->file_new_name[$key] = uniqid() . '.' $this->extension[$key];
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function moveFile($key)
    {
        if (!file_exists($this->destination_dir)) {
            mkdir($this->destination_dir, 0777, true);
        }
        $newName = rtrim($this->destination_dir, '/') . '/' $this->file_new_name[$key];
        if (is_uploaded_file($this->file_tmp_name[$key]) && move_uploaded_file($this->file_tmp_name[$key], $newName)) {
            return true;
        }
        $this->setError($key'上传失败!');
        return false;
    }
 
    /**
     * @return mixed
     */
    public function getFileName()
    {
        return $this->file_new_name;
    }
 
    /**
     * @return string
     */
    public function getDestinationDir()
    {
        return $this->destination_dir;
    }
 
    /**
     * @return mixed
     */
    public function getExtension()
    {
        return $this->extension;
    }
 
    /**
     * @return mixed
     */
    public function getFileSize()
    {
        return $this->file_size;
    }
 
}

html代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Upload Class</title>
</head>
<body>
<form action="4test.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" VALUE="10240000">
    <input type="file" name="imooc_file" id="">
    <input type="submit" value="上传">
 
</form>
 
 
</body>
</html>
1
调用类PHP文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
 
require('4fengzhuang.php');
 
//通过这些方法去设定
$upload new UploadFile('imooc_pic');
$upload->setDestinationDir('./uploads');
$upload->setAllowMime(['image/jpeg','image/gif','image/jpg']);
$upload->setAllowExt(['gif''jpeg','jpg']);
$upload->setAllowSize(2*1024*1024);
//设定好了之后 打印出来
if ($upload->upload()) {
    var_dump($upload->getFileName());
    var_dump($upload->getDestinationDir());
    var_dump($upload->getExtension());
    var_dump($upload->getFileSize());
else {
    var_dump($upload->getErrors());
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕小尤 2019-11-21 16:49:14

同学你好,多文件上传,请同学尝试使用源码中的upload_class文件中的代码。祝学习愉快!

guly 2019-11-20 17:34:34

老师是在改代码的基础上进行png图片的上传测试是可以成功的,建议在根据代码测试,并查看是否上传成功

http://img1.sycdn.imooc.com//climg/5dd508520904743b13790422.jpg

老师会对源码进行审核并修改,祝学习愉快!

  • 提问者 昵称加载中__ #1
    多文件 上传不了啊 你试试看呢 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test Upload Class</title> </head> <body> <form action="4文件上传类测试.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" VALUE="10240000"> <input type="file" name="imooc_file[]" id=""> //修改一 <input type="file" name="imooc_file[]" id=""> //修改一 <input type="submit" value="上传"> </form> </body> </html>
    2019-11-20 17:37:01
  • 提问者 昵称加载中__ #2
    而且这里 if (is_uploaded_file($this->file_tmp_name[$key]) && move_uploaded_file($this->file_tmp_name[$key], $newName)) { echo "上传成功!"; die(); 上传成功就结束程序 我试了一下 没法打印
    2019-11-20 17:39:12
guly 2019-11-20 16:07:10

你好,老师经过调试后的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
class UploadFile   //改文件修改比较多建议仔细查看
{
    /**
     *
     */
    const UPLOAD_ERROR = [
        UPLOAD_ERR_INI_SIZE => '文件大小超出了php.ini当中的upload_max_filesize的值',
        UPLOAD_ERR_FORM_SIZE => '文件大小超出了MAX_FILE_SIZE的值',
        UPLOAD_ERR_PARTIAL => '文件只有部分被上传',
        UPLOAD_ERR_NO_FILE => '没有文件被上传',
        UPLOAD_ERR_NO_TMP_DIR => '找不到临时目录',
        UPLOAD_ERR_CANT_WRITE => '写入磁盘失败',
        UPLOAD_ERR_EXTENSION => '文件上传被扩展阻止',
    ];
 
    /**
     * @var
     */
    protected $field_name//受保护的,只有本类或子类或父类中可以访问  文件名
 
    /**
     * @var string
     */
    protected $destination_dir;  //目录路径
 
    /**
     * @var array
     */
    protected $allow_mime;
 
    /**
     * @var array
     */
    protected $allow_ext;
 
    /**
     * @var
     */
    protected $file_org_name;
 
    /**
     * @var
     */
    protected $file_type;
 
    /**
     * @var
     */
    protected $file_tmp_name;
 
    /**
     * @var
     */
    protected $file_error;
 
    /**
     * @var
     */
    protected $file_size;
 
    /**
     * @var array
     */
    protected $errors;
 
    /**
     * @var
     */
    protected $extension;
 
    /**
     * @var
     */
    protected $file_new_name;
 
    /**
     * @var float|int
     */
    protected $allow_size;
 
    /**
     * UploadFile constructor.
     * @param $keyName
     * @param string $destinationDir
     * @param array $allowMime
     * @param array $allowExt
     * @param float|int $allowSize
     */
    public function __construct($keyName$destinationDir './uploads',
                                $allowMime = ['image/jpeg''image/gif','image/png'],
                                $allowExt = ['gif''jpeg','png'], $allowSize = 2 * 1024 * 1024) //如果成功,则该函数返回一个对象。如果失败,则返回 false。
    {  //添加了png,测试使用png图片上传可以上传成功,
 
        $this->field_name = $keyName;
        $this->destination_dir = $destinationDir;
        $this->allow_mime = $allowMime;
        $this->allow_ext = $allowExt;
        $this->allow_size = $allowSize;
    }
 
    /**
     * @param $destinationDir
     */
    public function setDestinationDir($destinationDir)
    {
 
        $this->destination_dir = $destinationDir;
    }
 
    /**
     * @param $allowMime
     */
    public function setAllowMime($allowMime)
    {
        $this->allow_mime = $allowMime;
    }
 
    /**
     * @param $allowExt
     */
    public function setAllowExt($allowExt)
    {
 
        $this->allow_ext = $allowExt;
    }
 
    /**
     * @param $allowSize
     */
    public function setAllowSize($allowSize)
    {
        $this->allow_size = $allowSize;
    }
 
    /**
     * @return bool
     */
    public function upload()
    {
        // 判断是否为多文件上传
        $files = [];
//      print_r($_FILES);
//        print_r("</br>");
//        print_r($_FILES[$this->field_name]);
//        print_r("</br>");
//        die();
//        if (is_array($_FILES[$this->field_name]['name'])) {
        if (is_array($_FILES[$this->field_name])) {
//            print_r($_FILES[$this->field_name]);die();
            foreach ($_FILES[$this->field_name] as $k => $v) {
                $files[$k] = $v;
//                $files[$k]['name'] = $v;
//                $files[$k]['type'] = $_FILES[$this->field_name]['type'][$k];
//                $files[$k]['tmp_name'] = $_FILES[$this->field_name]['tmp_name'][$k];
//                $files[$k]['error'] = $_FILES[$this->field_name]['error'][$k];
//                $files[$k]['size'] = $_FILES[$this->field_name]['size'][$k];
            }
        else {
            $files[] = $_FILES[$this->field_name];
        }
        foreach ($files as $key => $file) {
            // 接收$_FILES参数
            $this->setFileInfo($key$files);
            // 检查错误
            $this->checkError($key);
 
            // 检查MIME类型
            $this->checkMime($key);
 
            // 检查扩展名
            $this->checkExt($key);
 
            // 检查文件大小
            $this->checkSize($key);
 
            // 生成新的文件名称
            $this->generateNewName($key);
 
            if (count((array)$this->getError($files['error'])) > 0) {
                continue;
            }
            // 移动文件
            $this->moveFile($key,$files);
        }
        if (count((array)$this->errors) > 0) {
            return false;
        }
        return true;
    }
 
    /**
     * @return array
     */
    public function getErrors()
    {
        return $this->errors;
    }
 
    /**
     * @param $key
     * @return mixed
     */
    protected function getError($key)
    {
        if($key){
            return $this->errors[$key];
        }
 
    }
 
    /**
     *
     */
    protected function setFileInfo($key$files)  //用来设定 接收到$_FILES的参数
    {
        // $_FILES  name type temp_name error size
//        $this->file_org_name[$key] = $file['name'];
//        $this->file_type[$key] = $file['type'];
//        $this->file_tmp_name[$key] = $file['tmp_name'];
//        $this->file_error[$key] = $file['error'];
//        $this->file_size[$key] = $file['size'];
        $this->file_org_name[$key] = $files['name'];
        $this->file_type[$key] = $files['type'];
        $this->file_tmp_name[$key] = $files['tmp_name'];
        $this->file_error[$key] = $files['error'];
        $this->file_size[$key] = $files['size'];
    }
 
 
    /**
     * @param $key
     * @param $error
     */
    protected function setError($key$error)
    {
        $this->errors[$key][] = $error;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkError($key)
    {
        if ($this->file_error > UPLOAD_ERR_OK) {
            switch ($this->file_error) {
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                case UPLOAD_ERR_PARTIAL:
                case UPLOAD_ERR_NO_FILE:
                case UPLOAD_ERR_NO_TMP_DIR:
                case UPLOAD_ERR_CANT_WRITE:
                case UPLOAD_ERR_EXTENSION:
                    $this->setError($key, self::UPLOAD_ERROR[$this->file_error]);  //内部访问常量用self::
                    return false;
            }
        }
        return true;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkMime($key)
    {
        if (!in_array($this->file_type[$key], $this->allow_mime)) {
            $this->setError($key'文件类型' $this->file_type[$key] . '不被允许!');
            return false;
        }
        return true;
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function checkExt($key)
    {
        $this->extension[$key] = pathinfo($this->file_org_name[$key], PATHINFO_EXTENSION);
        if (!in_array($this->extension[$key], $this->allow_ext)) {
            $this->setError($key'文件扩展名' $this->extension[$key] . '不被允许!');
            return false;
        }
        return true;
    }
 
    /**
     * @return bool
     */
    protected function checkSize($key)
    {
        if ($this->file_size[$key] > $this->allow_size) {
            $this->setError($key'文件大小' $this->file_size[$key] . '超出了限定大小' $this->allow_size);
            return false;
        }
        return true;
    }
 
 
    /**
     * @param $key
     */
    protected function generateNewName($key)  //生成新的文件名
    {
        $this->file_new_name[$key] = uniqid() . '.' $this->extension[$key];
    }
 
 
    /**
     * @param $key
     * @return bool
     */
    protected function moveFile($key,$files)
    {
//        print_r($key);
        $newName = rtrim($this->destination_dir, '/') . '/' $this->file_new_name[$key];
 
        if (is_uploaded_file($this->file_tmp_name[$key]) && move_uploaded_file($this->file_tmp_name[$key], $newName)) {
           echo "上传成功!";
            die();
        }else{
            $this->setError($key'上传失败!');
        }
 
        return false;
    }
 
    /**
     * @return mixed
     */
    public function getFileName()
    {
        return $this->file_new_name;
    }
 
    /**
     * @return string
     */
    public function getDestinationDir()
    {
        return $this->destination_dir;
    }
 
    /**
     * @return mixed
     */
    public function getExtension()
    {
        return $this->extension;
    }
 
    /**
     * @return mixed
     */
    public function getFileSize()
    {
        return $this->file_size;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
require('fengzhuang.php');
//通过这些方法去设定
$upload new UploadFile('imooc_file');//与post中那么保持一致
$upload->setDestinationDir('./uploads');
$upload->setAllowMime(['image/jpeg','image/gif','image/jpg','image/png']);
$upload->setAllowExt(['gif''jpeg','jpg','png']);
$upload->setAllowSize(2*1024*1024);
//设定好了之后 打印出来
if ($upload->upload()) {
    var_dump($upload->getFileName());
    var_dump($upload->getDestinationDir());
    var_dump($upload->getExtension());
    var_dump($upload->getFileSize());
else {
    var_dump($upload->getErrors());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Upload Class</title>
</head>
<body>
<form action="test.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" VALUE="10240000">
    <input type="file" name="imooc_file" id=""//修改一
    <input type="submit" value="上传">
 
</form>
 
 
</body>
</html>

建议同学根据老师修改部分进行调试,

  • 提问者 昵称加载中__ #1
    还是不行啊 第一他不打印 文件名这些了 第二 <input type="file" name="imooc_file[]" id=""> //修改一 <input type="file" name="imooc_file[]" id=""> //修改一我用这个 无法上传多个文件 为什么教辅的源码都不对呢
    2019-11-20 17:08:04
  • 提问者 昵称加载中__ #2
    麻烦你们修改一下 课件里的源码 上传单一文件就会失败 上传两个一样的文件才能成功
    2019-11-20 17:13:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码