- <?php
- header(“Content-Type:text/html; charset=utf-8”);
- if($_POST[‘submit’]){
- $upfiles = new Upload();
- $upfiles->upload_file();
- }
- class Upload{
-
- public $upload_name;
- public $upload_tmp_name;
- public $upload_final_name;
- public $upload_target_dir;
- public $upload_target_path;
- public $upload_filetype ;
- public $allow_uploadedfile_type;
- public $upload_file_size;
- public $allow_uploaded_maxsize=10000000;
-
- public function __construct()
- {
- $this->upload_name = $_FILES[“file”][“name”];
- $this->upload_filetype = $_FILES[“file”][“type”];
- $this->upload_tmp_name = $_FILES[“file”][“tmp_name”];
- $this->allow_uploadedfile_type = array(‘jpeg’,’jpg’,’png’,’gif’,’bmp’,’doc’,’zip’,’rar’,’txt’,’wps’);
- $this->upload_file_size = $_FILES[“file”][“size”];
- $this->upload_target_dir=“./upload”;
- }
-
- public function upload_file()
- {
- $upload_filetype = $this->getFileExt($this->upload_name);
- if(in_array($upload_filetype,$this->allow_uploadedfile_type))
- {
- if($this->upload_file_size < $this->allow_uploaded_maxsize)
- {
- if(!is_dir($this->upload_target_dir))
- {
- mkdir($this->upload_target_dir);
- chmod($this->upload_target_dir,0777);
- }
- $this->upload_final_name = date(“YmdHis”).rand(0,100).‘.’.$upload_filetype;
- $this->upload_target_path = $this->upload_target_dir.“/”.$this->upload_final_name;
- if(!move_uploaded_file($this->upload_tmp_name,$this->upload_target_path))
- echo “<font color=red>文件上传失败!</font>”;
- }
- else
- {
- echo(“<font color=red>文件太大,上传失败!</font>”);
- }
- }
- else
- {
- echo(“不支持此文件类型,请重新选择”);
- }
- }
-
-
-
-
- public function getFileExt($filename){
- $info = pathinfo($filename);
- return $info[“extension”];
- }
-
- }
- ?>
- <form enctype=“multipart/form-data” method=“POST” action=“”>
- <input type=“file” name=“file”><input type=“submit” name=“submit” value=“上传”>
- </form>
php 文件上传