本文实例为大家分享了php上传文件功能的具体代码,供大家参考,具体内容如下
html:
1
2
3
4
5
|
< form action = "upload_file.php" method = "post" enctype = "multipart/form-data" > < label for = "file" >文件名:</ label > < input type = "file" name = "file" id = "file" >< br > < input type = "submit" name = "submit" value = "提交" > </ form > |
php:
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
|
<?php // 允许上传的图片后缀 $allowedExts = array ( "gif" , "jpeg" , "jpg" , "png" ); $temp = explode ( "." , $_FILES [ "file" ][ "name" ]); echo $_FILES [ "file" ][ "size" ]; $extension = end ( $temp ); // 获取文件后缀名 if ((( $_FILES [ "file" ][ "type" ] == "image/gif" ) || ( $_FILES [ "file" ][ "type" ] == "image/jpeg" ) || ( $_FILES [ "file" ][ "type" ] == "image/jpg" ) || ( $_FILES [ "file" ][ "type" ] == "image/pjpeg" ) || ( $_FILES [ "file" ][ "type" ] == "image/x-png" ) || ( $_FILES [ "file" ][ "type" ] == "image/png" )) && ( $_FILES [ "file" ][ "size" ] < 204800) // 小于 200 kb && in_array( $extension , $allowedExts )) { if ( $_FILES [ "file" ][ "error" ] > 0) { echo "err" ; } else { echo "上传文件名: " . $_FILES [ "file" ][ "name" ] . "<br>" ; echo "文件类型: " . $_FILES [ "file" ][ "type" ] . "<br>" ; echo "文件大小: " . ( $_FILES [ "file" ][ "size" ] / 1024) . " kB<br>" ; echo "文件临时存储的位置: " . $_FILES [ "file" ][ "tmp_name" ] . "<br>" ; // 判断当期目录下的 upload 目录是否存在该文件 // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777 if ( file_exists ( "../../../Content/UploadFiles/00/00/00/" . $_FILES [ "file" ][ "name" ])) { echo "err" ; } else { // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下 move_uploaded_file( $_FILES [ "file" ][ "tmp_name" ], "../../../Content/UploadFiles/00/00/00/" . $_FILES [ "file" ][ "name" ]); } } } else { echo "err" ; } ?> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/woaichirourou/p/7567438.html