froala图片明明上传成功了,还是会提示,遇到些错误,请在试试,原来还是php控制器的错误

最终发现是php的问题

正确的php如下

  public function fr_upload(){
      
        try {
          // File Route.
          $fileRoute = ROOT_PATH . 'public/uploads/';
        
          $fieldname = "multipartFile";
        
          
          // Get filename.
          $filename = explode(".", $_FILES[$fieldname]["name"]);
        //   halt($filename);
          // Validate uploaded files.
          // Do not use $_FILES["file"]["type"] as it can be easily forged.
          $finfo = finfo_open(FILEINFO_MIME_TYPE);
        
          // Get temp file name.
          $tmpName = $_FILES[$fieldname]["tmp_name"];
        
          // Get mime type.
          $mimeType = finfo_file($finfo, $tmpName);
        
          // Get extension. You must include fileinfo PHP extension.
          $extension = end($filename);
        
          // Allowed extensions.
          $allowedExts = array("gif", "jpeg", "jpg", "png", "svg", "blob");
        
          // Allowed mime types.
          $allowedMimeTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml");
        
          // Validate image.
          if (!in_array(strtolower($mimeType), $allowedMimeTypes) || !in_array(strtolower($extension), $allowedExts)) {
            throw new \Exception("File does not meet the validation.");
          }
        
          // Generate new random name.
          $name = sha1(microtime()) . "." . $extension;
        //   halt( dirname(__FILE__));
          $fullNamePath = $fileRoute . $name;
          // Save file in the uploads folder.
          move_uploaded_file($tmpName, $fullNamePath);

        //   halt($fullNamePath);
        
        //   // Check server protocol and load resources accordingly.
          if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {
             $protocol = "https://";
           } else {
             $protocol = "http://";
           }
          


          //按时间分类存储,如果文件夹不存在则建立; 这里必须加上----$this->,是为了控制器内部的方法互相调用
           // 由于是在uploads目录下创建,刚开始,我没写uploads,所以,文件地址一直不对。
             $fileNewPath=$this->fileTime($fullNamePath); 
             if(!file_exists('uploads/'.$fileNewPath)){ 
                mkdir('uploads/'.$fileNewPath); 
            } 
            // halt($fileNewPath);
            $xinFullNamePath=$fileRoute.$fileNewPath.'/'.$name;

        //  halt($fullNamePath);
        //   halt($xinFullNamePath);
          // 保存在新的地址里去
          $this->moveFile($fullNamePath,$xinFullNamePath);
         
        
          // Generate response.
          $response = new \StdClass;
           //$response->link = 'uploads/'.$fileNewPath.'/'.$name; 相对路径不行。必须是绝对路径
            $response->link =$protocol.$_SERVER["HTTP_HOST"].'/uploads/'.$fileNewPath.'/'.$name;
          //$response->link = 'http://www.wrsks.com/statics/images/dk/logo.jpg';
        
          // Send response.
          echo stripslashes(json_encode($response));
        
        } catch (Exception $e) {
           // Send error response.
           echo $e->getMessage();
           http_response_code(404);
        }
       
     }

        //  按时间进行存储图片-----
        //取得文件修改时间 
        public function fileTime($str){ 
        return date("Ymd",filemtime($str)); 
        } 
        // //移动文件至新地址,其中第二个路径,不是目录,是新的图片地址
        public function moveFile($file,$path){ 
        copy($file,$path); 
       
        unlink($file); 
      
        } 
     //  按时间进行存储图片-----
posted @ 2020-07-05 00:49  风意不止  阅读(338)  评论(0编辑  收藏  举报