zoukankan      html  css  js  c++  java
  • 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); 
          
            } 
         //  按时间进行存储图片-----
    
  • 相关阅读:
    时间转换(scanf的指定格式读入)
    RabbitMQ (五)主题(Topic)
    第01章 概述(1)
    bitmap与2-bitmap使用总结
    MyEclipse开启Jquery智能提示
    开启unity3D的学习之路
    《学习opencv》笔记——基本数据结构,CvMat,矩阵訪问
    使用squid架设自己的代理server
    Android KitKat 4.4平台开发-加入USB ADB和MTP功能支持
    Android:创建可穿戴应用
  • 原文地址:https://www.cnblogs.com/cn-oldboy/p/13237379.html
Copyright © 2011-2022 走看看