Reputation: 961
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?
Upvotes: 1
Views: 86
Reputation:
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.
Upvotes: 1