php文件上传实例
今天改进了下旗下几个网站的文件上传系统,顺便发点东西,全php代码,无js,文件类型根据后缀名判断,非mime判断,新建个up.php,代码如下:
<?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 "上传成功!" ; } ?>调用代码如下:
< form action = "up.php" method = "post" enctype = "multipart/form-data" > < input type = "file" name = "upload_file" /> < input type = "submit" value = "上传" />用mime类型限制有局限性,有些文件在上传是不是正常本身的mime,导致上传不成功,而用后缀名限制可以很好的解决这个问题.
查看更多关于php文件上传实例 - php上传下载的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29361