background image

php 文件上传综合实例分享

   本文介绍下,php 实现文件上传的一个综合实例,包括文件上传信息的获取、文件是否重
名的判断,以及中文乱码的问题,还有文件上传的封装代码。有需要的朋友参考下吧。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>php 文件上传-www.jbxue.com</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">    
  </head>     
  <body>
        <!--注意:1、要有 enctype,2、method="post"-->
    <form enctype="multipart/form-data" action="uploadProcess.php" method="post" >
        <table>
           <tr><td>请填写用户名</td><td><input type="text" name="username"></td></tr>
                     <tr><td> 请 简 单 介 绍 文 件 </td><td><textarea  rows="7"  cols="50"  name="fileintro" 
style="width:300px;"></textarea></td></tr>
            <tr><td>请上传你的文件</td><td><input type="file" name="myfile"></td></tr>
            <tr><td colspan="2"><input type="submit" value="上传"><td></tr>
        </table>
    </form>
  </body>
</html>
2、文件上传结果 uploadProcess.php
<?php
    //接收
    $username=$_POST['username'];
    $fileintro=$_POST['fileintro'];
    
    //echo $username.$fileintro;
    //获取文件信息
/*    echo "<pre>";
    print_r($_FILES);
    echo "</pre>";
*/    
    //获取文件的大小
    $file_size=$_FILES['myfile']['size'];
    if($file_size>2*1024*1024){
        echo "<script type='text/javascript'>window.alert('文件不能大于 2M')</script>";
        exit();
    }

    //获取文件类型
    $file_type=$_FILES['myfile']['type'];