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); 
          
            } 
         //  按时间进行存储图片-----
    
  • 相关阅读:
    STL特性总述——写在前面
    C++多线程框架
    C++内存管理之unique_ptr
    ubuntu文本模式/终端中文乱码解决
    log4net日志在app.config中assembly不起作用
    解决多线程委托二义性问题
    IIS 中文文件名下载会出现403访问被拒绝
    C# 异常:从作用域“”引用了“FiasHostApp.Entity.DBEntity.FIAS_RM_v1.ITraNetMgrUnitBaseInfoRecord”类型的变量“w”,但该变量未定义
    C# string.Split对于换行符的分隔正确用法
    knockoutJS+knockout.multimodels使用记录
  • 原文地址:https://www.cnblogs.com/cn-oldboy/p/13237379.html
Copyright © 2011-2022 走看看