本文实例为大家分享了PHP实现文件上传后台处理的具体代码,供大家参考,具体内容如下
后台服务器接收到的前端给其发送的文件后,处理流程如下:
文件上传是否有错误->判断文件类型->文件大小->保存文件到指定位置->其它处理...
具体实例
前端form表单:
1
2
3
4
5
6
7
8
9
10
11
12
|
< html > < body > < form action = "upload_file.php" method = "post" enctype = "multipart/form-data" > < label for = "file" >Filename:</ label > < input type = "file" name = "file" id = "file" /> < br /> < input type = "submit" name = "submit" value = "Submit" /> </ form > </ body > </ html > |
前端将文件传送给服务器后,服务器对文件的处理代码如下:
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
|
<?php if ((( $_FILES [ "file" ][ "type" ] == "image/gif" ) || ( $_FILES [ "file" ][ "type" ] == "image/jpeg" ) || ( $_FILES [ "file" ][ "type" ] == "image/pjpeg" )) && ( $_FILES [ "file" ][ "size" ] < 20000)) { if ( $_FILES [ "file" ][ "error" ] > 0) { echo "Return Code: " . $_FILES [ "file" ][ "error" ] . "<br />" ; } else { echo "Upload: " . $_FILES [ "file" ][ "name" ] . "<br />" ; echo "Type: " . $_FILES [ "file" ][ "type" ] . "<br />" ; echo "Size: " . ( $_FILES [ "file" ][ "size" ] / 1024) . " Kb<br />" ; echo "Temp file: " . $_FILES [ "file" ][ "tmp_name" ] . "<br />" ; if ( file_exists ( "upload/" . $_FILES [ "file" ][ "name" ])) { echo $_FILES [ "file" ][ "name" ] . " already exists. " ; } else { move_uploaded_file( $_FILES [ "file" ][ "tmp_name" ], "upload/" . $_FILES [ "file" ][ "name" ]); echo "Stored in: " . "upload/" . $_FILES [ "file" ][ "name" ]; } } } else { echo "Invalid file" ; } ?> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/angelina525/article/details/10300053