您的位置:澳门402永利com > 计算机 网络 > php简单文件上传代码,简单文件图片上传类

php简单文件上传代码,简单文件图片上传类

发布时间:2019-09-27 05:19编辑:计算机 网络浏览(133)

    这个文件上传类可以上传gif",".jpg",".jar",".jad",".mid",".mp3",".mid",".bmp",".wav",".rm",".wmv文件格式哦,如果你还想支持其它的话可以设计$this->file_type();就OK了,。

    <html>
    <head>
    <title>filemanage</title>
    </head>
    <body>
    <center>
    <p><h2>文件管理</h2></p><br>
    <table border=1><tr size=18>
    <td>文件ID</td><td>文件名称</td><td>文件大小</td><td>上传时间</td><td>下载</td><td>删除</td></tr>
    <?php
    include("conn.php");
    //echo '<table border=1>';
    $rs=$db->query("select * from filem order by f_id DESC");
    $i=1;
    while($row = $rs->fetch_assoc())      
    {
     $size=$row['f_size']/1024;
    echo "<tr><td>".$i++."</td><td>".$row['f_name']."</td><td>".number_format($size, 2, '.', '')."KB</td><td>".$row['f_date']."</td><td><a href=".$row['f_url']." target=_blank>下载</a></td><td><a href=del.php?id=".$row['f_id'].">删除</a></td></tr>"; 
    }
    echo '</table>';
    unset($rs);
    $db->close();
    ?>
    </center>
    <br><br><hr><br>
    <b><h2>uploadfile</h2></b>
    <br>
    <form enctype="multipart/form-data" action="" method="post">
    选择上传文件:<br><input name="userfile" type="file"><br>
    <input type="submit" value="发送">
    </form>
    <?php
     if(!$_FILES["userfile"]["name"])  exit;
    //echo $_FILES['userfile']['type'];
    if ($_FILES['userfile']['error'] > 0)
      {
        echo 'Problem: ';
        switch ($_FILES['userfile']['error'])
        {
          case 1:  echo 'File exceeded upload_max_filesize';  break;
          case 2:  echo '不能超过800M';  break;
          case 3:  echo 'File only partially uploaded';  break;
          case 4:  echo 'No file uploaded';  break;
        }
        exit;
      }
    else
    { //检查上传文件是否在允许上传的类型
       $tp = array("gif","jpeg","png","txt","doc","rar","zip","xls","bmp","wmv","mp3","flv","rmvb","avi");
    if (!in_array(strtolower(substr(strrchr($_FILES['userfile']['name'], '.'),1)), $tp))

    <?php
    class upload{

       {
        echo '文件类型错误,请重新选择文件!<br>只允许rar,zip,jpg,gif,txt,png,bmp,xls类型的文件';
        exit;
       }

    var $file_type;
    var $file_size;
    var $file_name;
    var $exname;
    var $datetime;

       $path="./file/";        //上传路径

    function up($post_name,$path){
     $this->file_name=$_FILES[$post_name]["name"];
     $this->exname=strtolower(substr($this->file_name,strrpos($this->file_name,".")));
     $this->file_size=$_FILES[$post_name]["size"];
     $this->file_type=array(".gif",".jpg",".jar",".jad",".mid",".mp3",".mid",".bmp",".wav",".rm",".wmv");
     $this->datetime=date("YmdHis");
     if(!in_array($this->exname,$this->file_type)){
      print("<script language='javascript'>");
      print("alert('格式不支持!');");
      print("history.back();");
      print("</script>");
      break;
     }elseif($this->file_size>"50000"){
      print("<script language='javascript'>");
      print("alert('图片大小不能超过50KB!');");
      print("history.back();");
      print("</script>");
      break;
     }else{
      move_uploaded_file($_FILES[$post_name]["tmp_name"],$path.$this->datetime.$this->exname);
      }
    }

       if(file_exists($path.$_FILES['userfile']['name']))    //判断文件是否存在
       {
        echo '文件已存在,请更改后重新上传!';
        exit;
       }

    function fileName(){
       $pic=$this->datetime.$this->exname;
       return $pic;
    }

       if($_FILES["userfile"]["name"])
       {
            $file1=$_FILES["userfile"]["name"];
            $file2 = $path.$file1;
            $flag=1;
       }
       if($flag)
           $result=move_uploaded_file($_FILES["userfile"]["tmp_name"],$file2);
       //特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件
       if($result)
          {
            
             $time=date("Y-m-d");
         //   $url=$patch.$name;
            $size=$_FILES["userfile"]["size"];
             include("conn.php");
             $rs=$db->query("insert into filem(f_name,f_url,f_date,f_size) values('$file1','$file2','$time','$size')");
        //     $rs=$db->query($sql);
           echo "<script language='javascript'>location='index.php';</script>";
          }
       unset($rs);
       $db->close();

    }
    ?>  

    }
    ?>
    </body>
    </html>

    本文由澳门402永利com发布于计算机 网络,转载请注明出处:php简单文件上传代码,简单文件图片上传类

    关键词:

上一篇:字符比较strcmp,php字符串比较函数

下一篇:没有了