php 上传类型限制的解决办法
内容摘要
这篇文章主要为大家详细介绍了php 上传类型限制的简单示例,具有一定的参考价值,可以用来参考一下。
对php上传类型限制,php文件上传实例对此感兴趣的朋友,看看idc笔记做的技术
对php上传类型限制,php文件上传实例对此感兴趣的朋友,看看idc笔记做的技术
文章正文
这篇文章主要为大家详细介绍了php 上传类型限制的简单示例,具有一定的参考价值,可以用来参考一下。
对php上传类型限制,php文件上传实例对此感兴趣的朋友,看看idc笔记做的技术笔记!全php代码,无js,文件类型根据后缀名判断,非mime判断。新建个up.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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <code class = "php" > /** * php上传类型限制,php文件上传实例 * * @param * @arrange 512-笔记网: 512PiC.com **/ <?php $uptype = array ( "jar" , "zip" ); //允许上传文件类型 $max_file_size =20480000; //上传文件大小限制, 单位BYTE $path_parts = pathinfo ( $_SERVER [ 'PHP_SELF' ]); //取得当前路径 $destination_folder = "files/" ; //上传文件路径 $name = "MuXi_" . date ( "Y-m-d_H-i-s" ); //保存文件名 if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' ) { $file = $_FILES [ "upload_file" ]; if (! is_uploaded_file ( $file [ "tmp_name" ])) //是否存在文件 { echo "文件不存在!" ; exit ; } $torrent = explode ( "." , $file [ "name" ]); $fileend = end ( $torrent ); $fileend = strtolower ( $fileend ); if (!in_array( $fileend , $uptype )) //检查上传文件类型 { echo "不允许上传此类型文件!" ; exit ; } if ( $max_file_size < $file [ "size" ]) //检查文件大小 { echo "文件太大,超过上传限制!" ; exit ; } if (! file_exists ( $destination_folder )) mkdir ( $destination_folder ); $filename = $file [ "tmp_name" ]; $image_size = getimagesize ( $filename ); $pinfo = pathinfo ( $file [ "name" ]); $ftype = $pinfo [extension]; $destination = $destination_folder . $name . "." . $ftype ; if ( file_exists ( $destination ) && $overwrite != true) { echo "同名文件已经存在了!" ; exit ; } if (!move_uploaded_file ( $filename , $destination )) { echo "移动文件出错!" ; exit ; } $pinfo = pathinfo ( $destination ); $fname = $pinfo [ basename ]; echo "上传成功!" ; } ?> /*** 来自php教程(www.idcnote.com) ***/ </code> |
1 2 3 4 5 | <code class = "html" > <form action= "up.php" method= "post" enctype= "multipart/form-data" > <input type= "file" name= "upload_file" /> <input type= "submit" value= "上传" /> </code> |
注:关于php 上传类型限制的简单示例的内容就先介绍到这里,更多相关文章的可以留意
代码注释