ajaxForm使用codeigniter插入图像到图像文件夹不工作

时间:2022-10-28 00:26:33

Hi i have this form which i uses ajaxForm to submit my images in the images folder. I already have the library for the ajaxForm when i tried to upload an image, ive noticed that the image i upload will not upload to assets/uploads folder ive tried to print_r the path but it displays the filename of an image but will not insert to assets/uploads folder here's my view below

嗨,我有这个表单,我使用ajaxForm提交我的图像文件夹中的图像。当我试图上传图片时,我已经有了ajaxForm的库,我注意到我上传的图片不会上传到assets / uploads文件夹我试过print_r路径但它显示图像的文件名但不会插入到assets / uploads文件夹在这里是我的视图

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
  <script src="http://malsup.github.com/jquery.form.js"></script> 
  <script> 
      jQuery(document).ready(function($){
          $("#uploadPhoto").change(function(){
                var val = $(this).val();
                switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
                  case 'gif': case 'jpg': case 'png': case 'jpeg':
                   $("#ptxt").fadeOut();
                   $("#buttonGallery").show();
                  break;
                  default:
                    $(this).val('');
                    // error message here
                    $("#ptxt").fadeIn();
                    $("#buttonGallery").hide();
                    break;
                }
              }); 

              $('#send_form').ajaxForm({  
                  target: '.result',
                  success: function(data) {

                  }      
              }); 


      });



    </script> 



<form id="send_form" enctype="multipart/form-data" action="<?php echo base_url().'admin/rooms/addGallery'?>" method="post" >                          
                          <div class="row">
                            <div class="col-md-4">
                              <div><h3>Gallery</h3></div>
                                                            <div class="alert alert-danger" id="ptxt">
                                                                <p>Invalid file type</p>
                                                            </div>
                              <div class="form-group">
                                <label for="uploadPhoto">Upload photo</label>
                                <input type="file" name="uploadPhoto" id="uploadPhoto" placeholder="Enter ">
                              </div>
                              <div class="form-group">
                                <label for="photoTitle">Photo title</label>
                                <input type="text" name="photoTitle" class="form-control" id="photoTitle" placeholder="Photo title " >
                              </div>
                              <div class="form-group">
                                <label for="photoDescription">Photo description</label>
                                <textarea name="photoDescription" class="form-control" id="photoDescription" placeholder="Photo description " rows="5"></textarea>
                              </div>                                                            
                            </div>                            
                          </div>
                                                    <input type="hidden" name="galleryId" id="galleryId" value="<?php echo $getRoomDetails->id; ?>">
                          <button type="submit" name="buttonGallery" id="buttonGallery" class="btn btn-primary">Save</button>
                        </form>

My controller

public function addGallery(){
        if($this->upload->do_upload('uploadPhoto')){
            $data['upload_data'] = $this->upload->data();          

            $uploadSuccess = $data['upload_data'];

            $raw_name = $uploadSuccess['file_name'];

            $this->load->library('image_lib');

            $file_name = $raw_name;

            $this->load->library('image_lib');

            $image_path = 'assets/uploads/' .$file_name;
            echo $image_path; exit;
            $config['image_library'] = 'gd2';
            $config['source_image'] =  $image_path;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 150;
            $config['height'] = 110;
            $config['new_image'] = 'thumb_'.$file_name;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }


    }

i added a config in the public function __construct just to make sure

我在公共函数__construct中添加了一个配置,以确保

public function __construct(){
        parent::__construct();
         $config['upload_path'] = 'assets/uploads/';
    // set the filter image types
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '9000';
    $config['encrypt_name']  = TRUE;

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

    $this->upload->initialize($config);
    $this->upload->set_allowed_types($config['allowed_types']);
    $data['upload_data'] = '';


    }

can someone help me figured this thing out?TIA

有人可以帮我解决这个问题吗?TIA

2 个解决方案

#1


there is problem in your controller file

控制器文件中存在问题

you upload you file before you config the setting

在配置设置之前上传文件

your controller code would be

您的控制器代码将是

<?php
public function addGallery(){
$data['upload_data'] = $this->upload->data();

$uploadSuccess = $data['upload_data'];

$raw_name = $uploadSuccess['file_name'];

$image_path = 'assets/uploads/' . $file_name;
 $config['upload_path'] = 'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

if (!$this->upload->do_upload()) {
    $error = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);
} else {
    $data['upload_data'] = $this->upload->data();          

            $uploadSuccess = $data['upload_data'];
            $raw_name = $uploadSuccess['file_name'];
            $file_name = $raw_name;
            $this->load->library('image_lib');

            $image_path = 'assets/uploads/' .$file_name;
            $config['image_library'] = 'gd2';
            $config['source_image'] =  $image_path;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 150;
            $config['height'] = 110;
            $config['new_image'] = 'thumb_'.$file_name;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
}
}

and remove upload part from controller constructor

并从控制器构造函数中删除上传部分

#2


Try giving the full path for your

尝试为您提供完整的路径

$config['upload_path']

I believe that you need to give the full path in order for your image to get saved. To get the full path you can use the function

我相信您需要提供完整路径才能保存图像。要获得完整路径,您可以使用该功能

getcwd()

to get your current working directory and to that you need to concat the remaining of your folder.

获取当前的工作目录,然后您需要连接文件夹的剩余部分。

Also initialize your config array before you load the helper.

在加载帮助程序之前,还要初始化配置数组。

#1


there is problem in your controller file

控制器文件中存在问题

you upload you file before you config the setting

在配置设置之前上传文件

your controller code would be

您的控制器代码将是

<?php
public function addGallery(){
$data['upload_data'] = $this->upload->data();

$uploadSuccess = $data['upload_data'];

$raw_name = $uploadSuccess['file_name'];

$image_path = 'assets/uploads/' . $file_name;
 $config['upload_path'] = 'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

if (!$this->upload->do_upload()) {
    $error = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);
} else {
    $data['upload_data'] = $this->upload->data();          

            $uploadSuccess = $data['upload_data'];
            $raw_name = $uploadSuccess['file_name'];
            $file_name = $raw_name;
            $this->load->library('image_lib');

            $image_path = 'assets/uploads/' .$file_name;
            $config['image_library'] = 'gd2';
            $config['source_image'] =  $image_path;
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 150;
            $config['height'] = 110;
            $config['new_image'] = 'thumb_'.$file_name;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
}
}

and remove upload part from controller constructor

并从控制器构造函数中删除上传部分

#2


Try giving the full path for your

尝试为您提供完整的路径

$config['upload_path']

I believe that you need to give the full path in order for your image to get saved. To get the full path you can use the function

我相信您需要提供完整路径才能保存图像。要获得完整路径,您可以使用该功能

getcwd()

to get your current working directory and to that you need to concat the remaining of your folder.

获取当前的工作目录,然后您需要连接文件夹的剩余部分。

Also initialize your config array before you load the helper.

在加载帮助程序之前,还要初始化配置数组。