1

I'm trying to upload multiple image using codeigniter thats actually working fine, But I want to store images to specific directory. My directory structure is

uploads/real/

inside this directory I want to create one more directory with the name of userid so directory structure becomes

uplods/real/20

I added logic and written some code like below: Consider $path="real", and $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();

And Browser Output is :

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_______

I'm not getting any error on printing print_r($error); But image is not uploaded to my directory WHY?

11
  • 1
    Might be happening because you are not telling it to! Can you see that you are setting your upload path to '/'. It says so in your code and also in your output. Commented Nov 18, 2016 at 5:37
  • When I set upload path to [upload_path] => uploads/real/18 I'm getting error from error array [error] => The upload path does not appear to be valid. Commented Nov 18, 2016 at 5:40
  • Do your paths get created when you use mkdir? Do they exist? Does your base folder have the correct permissions? Commented Nov 18, 2016 at 5:46
  • Yes path is created : The directory with userid 20 is created inside folder name 'real' Commented Nov 18, 2016 at 5:48
  • Well that being the case.. You are setting $config['upload_path']= "/"; Shouldn't that be the path you want to save your images in? ie the ones you are creating! Commented Nov 18, 2016 at 5:49

1 Answer 1

1

Try using FCPATH for uploads sometimes works better.

FCPATH: Path to the front controller (this file) (root of CI)

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

Or try

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

Make sure you set 0777 for image uploads folders.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.