這是一款完整的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
- /*
- * $name; 上傳文件名
- * $size: 上傳文件大小
- * $path; 文件原路徑
- * $newpath: 設置新路徑
- * $not: 禁止上傳的文件類型數組
- * $notsize: 限制文件大小的值
- * $move: 上傳文件源
- *
- */
- 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.49028c.com/','exe,rar',5);
新聞熱點
疑難解答