【问题标题】:Unable to upload image in codigniter, getting an wrong path error无法在 codeigniter 中上传图片,路径错误
【发布时间】:2021-01-30 09:00:47
【问题描述】:

我正在一个项目中尝试使用 jquery ajax + codeigniter 上传图片。

不知道为什么我会遇到以下问题:

错误: 上传路径似乎无效。

下面的代码有什么我遗漏的吗?

upload_product_image()是处理上传任务的方法

public function upload_product_image()
  {
    if(isset($_FILES["file"]["name"]))
     {
      $config['upload_path'] = 'upload/';
      $config['allowed_types'] = 'jpg|jpeg|png|gif|svg';
      $config['max_width'] = 0;
      $config['max_height'] = 0;
      $config['max_size'] = 0;
      $config['encrypt_name'] = TRUE;

      $this->load->library('upload', $config);

      if(!$this->upload->do_upload('file'))
      {
         echo 'Error: '. $this->upload->display_errors();
         echo '<hr>';
         echo $config['upload_path'];
      }
      else
      {
        $arr_image = array('upload_data' => $this->upload->data());
        print_r($arr_image);
      }
     }
  }

以下代码是HTML代码

<div class="card mb-2">
    <a class="card-link" data-toggle="collapse" href="#collapseone">
    <div class="card-header">
         Upload Product Image(One) <span class="float-right text-danger">Size(192X138)</span>
    </div>
    </a>
    <div id="collapseone" class="collapse show" data-parent="#accordion">
      <div class="card-body">
        <div class="form-group row" >
          <div class="col-sm-3 text-center" style="position:relative;">
            <img id="product_img_one_img_preview" src="upload/default/coming_soon.svg" class="w-100 img-fluid img-thumbnail" />
            <a href="#" class="btn btn-primary mt-2" onclick="$('#product_img_one_box').trigger('click'); return false;"><i class="fa fa-upload"></i> Upload file</a>
              <div id="product_img_one_loader"></div>
             </div>
             <div class="col-sm-9 my-auto">
               <input type="text" class="form-control" value="upload/default/coming_soon.svg" name="product_img_one" id="product_img_one" class="form-control" />
               <input type="file" name="" class="form-control-file border hidden" id="product_img_one_box">
             </div>
           </div>
         </div>
       </div>

以下是我使用的jQuery代码

 <script>
     $(document).ready(function() {
       $("#product_img_one_loader").hide();
       $('#product_img_one_box').on('change', function(){
            var fd = new FormData();
            var files = $(this)[0].files[0];
            fd.append('file', files);
            console.log(fd);
            $.ajax({
                url: 'https://mywebsite.com/shop/admin/upload_product_image',
                type: "post",
                data: fd,
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $("#product_img_one_loader").html('<div class="lds-facebook" style="position:absolute;top: 35%;left: 40%;"><div></div><div></div><div></div></div>');
                    $("#product_img_one_loader").show();

                },
                success: function(response) {
                    $("#product_img_one_loader").html('');
                    // $("#product_img_one_loader").hide();

                    $("#product_img_one_loader").html(response);

                    // alert(response);
                    // if (response != 0) {
                    //
                    // } else {
                    //   alert('file not uploaded');
                    // }
                },
                complete: function(data) {
                  // $("#product_img_one_loader").html('');
                  // $("#product_img_one_loader").hide();
                }
            });

          });
     });

【问题讨论】:

  • 确保目录是可写的。上面设置了什么chmod?
  • 由0755自动设置

标签: php jquery codeigniter file-upload frameworks


【解决方案1】:

上传路径必须定义为绝对服务器路径,而不是相对路径。

代替$config['upload_path'] = 'upload/'; 试试$config['upload_path'] = FCPATH.'upload/';

这个小改动将强制上传到upload 目录,该目录与前端控制器(CI 的主index.php 文件)位于同一目录中。

您当前的代码正在寻找与您的控制器 (application/controllers/upload/) 位于同一位置的 upload 目录,这不是您想要的。

另外,检查目录及其内容是否由 PHP 进程拥有和写入(755 是一个很好的起点)...一个简单的-ls -l 将为您提供有关此问题所需的所有信息

【讨论】:

  • 我会扩展这个答案,以确保您的文件夹具有正确的 chmod 和所有权。如果这 3 件事是正确的,那么您应该没有问题。
猜你喜欢
  • 2020-12-31
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 1970-01-01
  • 2020-06-12
  • 2017-10-04
  • 1970-01-01
相关资源
最近更新 更多