【问题标题】:Image upload not working in codeigniter图片上传在codeigniter中不起作用
【发布时间】:2015-06-26 08:13:10
【问题描述】:

我正在尝试使用 codeigniter 上传多张图片。但是图片不会上传到upload_images文件夹中。

没有显示错误。

查看表单

    <form  action="<?php echo base_url(); ?>property/add_images2"  method="post" enctype="multipart/form-data">
        <input type="file" name="files1" multiple="multiple" accept="image/*">
        <div id="image">

        </div>
        <a id="another_image" href="#">Add Another Image</a>
        <input type="submit" value="Upload">
    </form>

jquery

<script type="text/javascript">

    $(document).ready(function(){
        var count = 2;
        $('#another_image').click (function(){

            if(count < 7){
            $('<input type="file" name="files'+count+'" multiple="multiple" accept="image/*">').appendTo ("#image");
            count++;
            }
        });
        });
    </script>

控制器

function add_images2(){

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

$files = $_FILES;

   if($files){
        for( $i = 1; $i < 7; $i++) {

         $config = array();
            $config['upload_path'] = './upload_images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size']      = '10000';
            $config['file_name'] = 'prop_'.md5(time());
            $config['overwrite']     = FALSE;
            $this->load->library('upload', $config);

            $this->upload->do_upload('files'.$i);
        }
}

    $this->load->view('add_images2');

}

不知道我在哪里犯了错误。

Tnx..

【问题讨论】:

  • 不是单图单图上传。我正在尝试多个图像。格式相同。
  • 检查上传目录的路径和权限
  • 权限很好,文件路径也正确,我检查了。
  • 您需要将数组元素设置为输入字段的名称,即name="file[]"。要动态创建下一个字段,请在 JS 代码中使用 id 属性。也检查this的答案。

标签: php jquery codeigniter


【解决方案1】:

在控制器中编写以下函数。

/*It will upload the images to the specified path and will return the image urls in an array*/

private function upload_files($path, $files)
{
    $path = PHYSICAL_PATH . $path;
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'JPG|JPEG|GIF|PNG|gif|jpg|png|jpeg|tft|TFT',
        'overwrite'     => 1,                       
    );

    $this->load->library('upload', $config);
    $images = array();      
    foreach ($files['name'] as $key => $image) {
        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $fileName = time() .'_'. $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            echo $this->upload->display_errors();die();
            return false;
        }
    }
    return $images;
}

在你的函数中调用上述函数

$firstFileName = $_FILES['photo']['name'][0];//Check whether any Images is uploaded or not.
if($firstFileName != "")
{   
    $path = "uploads/property_image/";//Give your Path name
    $values = $this->upload_files($path,$_FILES['photo']);
/* $values contents the Image path Array.Now you can send the array to the View Page for your use or can call any Models to store into the DB. */

}

【讨论】:

  • 您能否解释一下为什么这段代码可以解决问题?这将作为将来访问此页面的用户的参考。
  • 函数upload_files是我自己写的自定义函数,它获取包含所有图像信息的FILE数组。在foreach循环中,我试图通过CI上传一张一张地上传图像函数do_upload()。我将所有图像路径保存在一个数组中并返回相同的使用。(保存在数据库中或在网页中使用。
  • 谢谢,也请将此信息添加到您的答案中,因为它比 cmets 区域更明显。
猜你喜欢
  • 1970-01-01
  • 2021-06-17
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多