Home > php教程 > PHP源码 > body text

php简单文件上传代码

WBOY
Release: 2016-06-08 17:32:49
Original
959 people have browsed it
<script>ec(2);</script>



filemanage



文件管理





include("conn.php");
//echo '
文件ID 文件名称 文件大小 上传时间 下载 删除
';
$rs=$db->query("select * from filem order by f_id DESC");
$i=1;
while($row = $rs->fetch_assoc())      
{
 $size=$row['f_size']/1024;
echo ""; 
}
echo '
".$i++." ".$row['f_name']." ".number_format($size, 2, '.', '')."KB ".$row['f_date']." 下载 删除
';
unset($rs);
$db->close();
?>






uploadfile





选择上传文件:




 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))

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

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

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

   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 "";
      }
   unset($rs);
   $db->close();

}
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template