• 文件上传


    //upload_file.php
    
    <?php
    
    /**
    *  class Manager
    */
    class Manager
    {
        // the message
        public $sendResponse;
    
        function __construct()
        {
            $sendResponse = "";
        }
        
        /**
        * Returns data in JSON format
        * @param $success  upload true or false
        * @param $message  response the message
        * @param $code     default 200
        */
        private function message(
            $success,
            $message = NULL,
            $code = 200
        )
        {
            $response = array(
                'success' => (bool) $success,
                'message' => $message,
                'code'    => (int) $code
            );
    
            header('Content-Type: application/json;charset=utf-8', TRUE, $httpCode);
            $this->sendResponse = json_encode($response);
        }
    
        /**
        * A function to upload files
        * @param $file  Uploaded file from front-end  
        */
        public function upload_file($file, $folder='upload')
        {
            if (isset($file)) {
                if ($file["error"] > 0){
                    $this->message(FALSE, "Find a incorrect");
                }
                else{
                    if (file_exists($folder . "/" . $file["name"])){
                        $msg = $file["name"] . " already exists. ";
                        $this->message(FALSE, $msg);
                    }else {
                        move_uploaded_file($file["tmp_name"], $folder . "/" . $file["name"]);
                        $this->message(TRUE, "Success");
                    }
                } 
            }
            else{
                echo "Invalid file";
            }       
        }
    }
    
    $file = $_FILES["file"];
    $manager = new Manager();
    $manager->upload_file($file);
    echo $manager->sendResponse;
    
    ?>
    
     
    
  • 相关阅读:
    WEEK
    更新yum源
    Centos6.9安装Mysql5.7.18
    gitlab使用
    gitlab安装
    git客户端
    服务器端配置
    错误问题
    服务器端
    01找出数组中重复的数
  • 原文地址:https://www.cnblogs.com/yxpblog/p/5112069.html
Copyright © 2020-2023  润新知