【问题标题】:Codeigniter not uploading image files in png formatCodeigniter 不上传 png 格式的图像文件
【发布时间】:2018-05-13 04:05:16
【问题描述】:

Codeigniter 3.1.6 是我正在使用的版本。当我尝试以 jpg 格式上传文件时,它是成功的,但是以 png 格式上传文件不起作用这里是我的代码。我正在使用 dropzone.js 进行文件上传

观看次数

<form action="admin/addimg" enctype="multipart/form-data" class="dropzone" style="margin-bottom: 5%; border: 2px dashed #0087F7; border-radius: 5px; background: white;" id="imgupld"></form>

<script>
Dropzone.options.imgupld = {
    paramName: "file",
    maxFilesize: 20, // MB
    acceptedFiles: "image/*", // Accept images only
    dictDefaultMessage: "Drag your image here"
};
</script>

控制器

function addimg()
    {
        if (!empty($_FILES))
        {
            $tempFile = $_FILES['file']['tmp_name'];
            $targetPath = './assets/img/';
            $targetFile = $targetPath . $_FILES['file']['name'];
            move_uploaded_file($tempFile, $targetFile);
        }
    }

【问题讨论】:

标签: php codeigniter file-upload dropzone.js


【解决方案1】:

这可以通过使用 CI 上传库轻松完成。首先你需要在 config->autoload.php 中加载库

    $autoload['libraries'] = array('upload');

或在您的控制器中。示例

    class ImageUpload extends CI_Controller
    {
       public function Upload()
       {
          if($_FILES["uploadImage"]["error"]==4)//check if user select input any image to upload
          {
               $config['Upload_Path']='';//File upload path
               $config['allowed_types']='jpeg|jpg|png';//set the preferred  file formats here
               $config['max_size']=100000;// Max File Size

               if($this->upload->do_upload('uploadImage'))//uploading the data
                {
                    $uploadData=$this->upload->data();//get the details of uploaded data
                }else
                {
                   $error=$this->upload->display_errors();//this return the error message if any error thrown 
                }
          }
       }
    }

【讨论】:

  • 只是为了指出你的类命名错误在最新版本的 codeigniter 只有第一个字母应该是大写的文件名和类名在这里解释或者虽然它可能在某些系统上工作最好将它命名为 codeigniter 方式如用户指南codeigniter.com/user_guide/general/styleguide.html#file-naming
  • 我看到错误证明我的文件超过了 php 的最大文件大小,感谢您的代码
猜你喜欢
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多