php文件上传代码
这是一款完整的php文件上传实例代码,支持上传的类型可以创建类时自定义,可支持的上传文件类型,代码如下:
- <form name="form1" enctype="multipart/form-data" method="post" action="">
- <label for="filefield"></label>
- <input type="file" name="filefield" id="filefield">
- <input type="submit" name="button" id="button" value="上传文件">
- </form>
- <?php
-
-
-
-
-
-
-
-
-
-
-
- class fileupload {
- public $name;
- public $size;
- public $path;
- public $newpath;
- public $not = array();
- public $notsize;
- public $move;
- public $allfile = array();
- function __construct($name,$size,$path,$newpath,$not,$notsize) {
- $this ->name = $name;
- $this ->size = $size/1048576;
- $this ->path = $path;
- $this ->newpath = $newpath;
- $this ->not = explode(',',$not);
- $this ->notsize = $notsize;
- $this ->upload();
- }
-
-
-
-
-
- function upload(){
- if(!file_exists($this->newpath)){
- echo "<script>alert('该目录不存在!')</script>";
- return;
- }else{
- $arr = explode('.',$this->name);
- if(in_array($arr[1],$this->not)){
- echo "<script>alert('该类型文件禁止上传!')</script>";
- return;
- }else if($this->name == ''){
- echo "<script>alert('请选择上传的文件!')</script>";
- return;
- }else if($this->size>$this->notsize){
- echo "<script>alert('上传文件超过规定大小!')</script>";
- return;
- }else if(file_exists("$this->newpath"."$this->name")){
- echo "<script>alert('该文件已经存在!')</script>";
- return;
- }
- else{
- $this->move = move_uploaded_file($this->path,$this->newpath.$this->name);
- $this->move();
- }
- }
- }
-
-
-
- function move(){
- if($this->move){
- echo "<script>alert('文件上传成功!')</script>";
- return;
- }else{
- echo "<script>alert('上传失败!')</script>";
- return;
- }
- }
- }
-
- $fu = new fileupload($array[name],$array[size],$array[tmp_name],'./www.phpfensi.com/','exe,rar',5);