如何使用codegniter上传多个图像

时间:2016-11-18 05:32:12

标签: php codeigniter file-upload

我尝试使用codeigniter上传多个图像,这样可以正常工作,但我想将图像存储到特定目录。 我的目录结构是

  

上传/真实/

在这个目录中我想再创建一个名为userid的目录,这样目录结构就变成了

  

uplods /真实/ 20

我添加了逻辑并编写了如下代码: 考虑$ path =" real"和$ userid =" 20"

function image_upload($path,$userid)
    {
        $basepath = "uploads/".$path;
        echo "base-path > ".$basepath;
        echo "<br>working-dir > ".getcwd();
        chdir($basepath);
        echo "<br>working-dir > ".getcwd();
        if(!file_exists($userid)){
            mkdir($userid);
            chdir($userid);
            echo "<br>Working-dir > ".getcwd();
            $filesCount = count($_FILES['image']['name']);
            echo "<br> FileCount > ".$filesCount;
            for($i = 0; $i < $filesCount; $i++){
                $_FILES['img']['name'] = $_FILES['image']['name'][$i];
                $_FILES['img']['type'] = $_FILES['image']['type'][$i];
                $_FILES['img']['tmp_name'] = $_FILES['image']['tmp_name'][$i];
                $_FILES['img']['error'] = $_FILES['image']['error'][$i];
                $_FILES['img']['size'] = $_FILES['image']['size'][$i];

                echo "<br>FILES-Array > <pre>";
                print_r($_FILES);
                echo "<pre>";

                $config['upload_path']= "/";
                $config['file_name']=$userid .$i. '.jpg';
                $config['allowed_types']= 'jpg|png';
                $config['max_size']= 2048;

                echo "<br>CONFIG-Array > <pre>";
                print_r($config);
                echo "<pre>";

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

                if ( ! $this->upload->do_upload('img'))
                {
                    $error = array('error' => $this->upload->display_errors());
                    echo "<br>UPLOAD-ERROR > <pre>";
                    print_r($error);
                    echo "<pre>";
                }
                else
                {
                    $da = array('upload_data' => $this->upload->data());
                    echo "_________IMAGE UPLOADED_______";
                }
            }

        }else{
            echo "<br>Folder Exist ...";
        }
        die();

浏览器输出为:

base-path > uploads/real
working-dir > C:\wamp64\www\ob_03
working-dir > C:\wamp64\www\ob_03\uploads\real
Working-dir > C:\wamp64\www\ob_03\uploads\real\20
FileCount > 1
    FILES-Array >
Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => abhi.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => C:\wamp64\tmp\php336F.tmp
                )

            [error] => Array
                (
                    [0] => 0
                )

            [size] => Array
                (
                    [0] => 72125
                )

        )

    [img] => Array
        (
            [name] => abhi.jpg
            [type] => image/jpeg
            [tmp_name] => C:\wamp64\tmp\php336F.tmp
            [error] => 0
            [size] => 72125
        )

)
CONFIG-Array > 
Array
(
    [upload_path] => /
    [file_name] => 180.jpg
    [allowed_types] => jpg|png
    [max_size] => 2048
)
_________IMAGE UPLOADED_______

我在打印时没有收到任何错误print_r($error);但是图片没有上传到我的目录为什么?

1 个答案:

答案 0 :(得分:1)

尝试使用FCPATH进行上传有时会更好。

FCPATH:前端控制器的路径(此文件)(CI的根目录)

$config['upload_path'] = FCPATH . 'uploads/real/18/';

或尝试

$config['upload_path'] = './uploads/real/18/';

确保为图片上传文件夹设置0777。