Codeigniter — Загрузить фото с формой добавления пользователя

Я хочу знать…

Я никогда не могу загрузить фото в свою папку и базу данных. Я пробовал это тысячу раз. В чем я ошибаюсь, пожалуйста, помогите мне в этом.

Я могу добавить пользователя хорошо. Это не проблема. Но я попробовал это с загрузкой фотографий, которые никогда не загружали ни одной фотографии. Мои коды:

контроллер:

     public function insert () {

$config['upload_path'] = './upload/user/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 1024;
$config['max_width'] = 100;
$config['max_height'] = 100;
$config['file_name'] = rand(100,999);
$config['overwrite'] = false;

$this->load->library('upload', $config);
if(!$this->upload->do_upload('userImg')) {

$this->load->view('user');
}


$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => $this->input->post("userSurname"),
"userImg" => $this->input->post("userImg"),
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$insert = $this->db->insert("user", $data);
if($insert) {
redirect(base_url("user"));
}else {

redirect(base_url("user"));

}

}

и моя страница просмотра ДОБАВИТЬ ПОЛЬЗОВАТЕЛЯ

<form class="cmxform form-horizontal tasi-form" id="signupForm" method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>user/insert">
<div class="form-group last">
<label class="control-label col-md-3">Avatar Upload:</label>
<div class="col-md-9">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
<img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" />
</div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
<span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
<input type="file" class="default" name="userImg" />
</span>
</div>
</div>
<span class="label label-danger">NOTE!</span>
<span>
Please Upload Your Profile Photo!
</span>
</div>
</div>
<div class="form-group ">
<label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class=" form-control" id="username" name="userUsername" type="text" required="required" />
</div>
</div>
<div class="form-group ">
<label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="password" name="userPass" type="password" required="required" />
</div>
</div>
<div class="form-group ">
<label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="email" name="userEmail" type="email" required="required" />
</div>
</div>

…..

Помоги мне за это. Спасибо вам всем.


РЕДАКТИРОВАТЬ ПОЛЬЗОВАТЕЛЬСКИЙ КОНТРОЛЛЕР:

public function update($userId) {
if (isset($_FILES)) {
$config['upload_path'] = './upload/user';
$config['allowed_types']        = 'jpeg|jpg|png|gif';
$config['max_size'] = 5000;
$config['max_width'] = 4024;
$config['max_height'] = 4068;
$new_name = $this->input->post("userUsername");
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userImg'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else{
$upload_data = $this->upload->data();
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userImg" => $upload_data['file_name'],
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}
else{
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}

РЕДАКТИРОВАТЬ ПОЛЬЗОВАТЕЛЬСКИЙ ВИД:

<div class="form">
<form class="cmxform form-horizontal tasi-form" id="signupForm" method="post" action="<?php echo base_url("user/update/$user->userId"); ?>">
<div class="form-group last">
<label class="control-label col-md-3">Avatar Upload:</label>
<div class="col-md-9">
<div class="fileupload fileupload-new" data-provides="fileupload" value="<?php echo $user->userImg; ?>">
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
<img src="<?php echo base_url().'upload/user/'.$user->userImg; ?> alt="" />
</div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
<span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
<input type="file" class="default" name="userImg" />
</span>
</div>
</div>
<span class="label label-danger">NOTE!</span>
<span>
Please Upload Your Profile Photo!
</span>
</div>
</div>
<div class="form-group ">
<label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class=" form-control" id="username" name="userUsername" type="text" value="<?php echo $user->userUsername; ?>" />
</div>
</div>
<div class="form-group ">
<label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="password" name="userPass" type="password" value="<?php echo $user->userPass; ?>" readonly />
</div>
</div>
<div class="form-group ">
<label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="email" name="userEmail" type="email" value="<?php echo $user->userEmail; ?>" />
</div>
</div>
<div class="form-group ">
<label for="firstname" class="control-label col-lg-2">First Name <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class=" form-control" id="firstname" name="userName" type="text" value="<?php echo $user->userName; ?>" />
</div>
</div>
<div class="form-group ">
<label for="lastname" class="control-label col-lg-2">Last Name <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="lastname" name="userSurname" type="text" value="<?php echo $user->userSurname; ?>" />
</div>
</div>

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Choose Language <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userLang" class="form-control">
<option value="<?php echo $user->userLang; ?>"><?php echo $user->userLang; ?></option>
<option value="en">English</option>
<option value="ar">Arabic</option>
</select>
</div>
</div>
<!-- /basic select -->

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">User Type <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userType" class="form-control">
<option value="<?php echo $user->userType; ?>"><?php echo $user->userType; ?></option>
<option value="1">Admin = 1</option>
<option value="2">Yönetici = 2</option>
<option value="3">Departman Sorumlusu = 3</option>
<option value="4">Operatör = 4</option>
<option value="5">Stajyer = 5</option>
</select>
</div>
</div>
<!-- /basic select -->

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userStatus" class="form-control">
<option value="<?php echo $user->userStatus; ?>"><?php echo $user->userStatus; ?></option>
<option value="1">Active</option>
<option value="0">Deactive</option>
</select>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit" value="Upload">Edit User</button>
<button class="btn btn-default" type="reset">Reset</button>
<button class="btn btn-danger" type="cancel">Cancel</button>
</div>
</div>
</form>
</div>

-1

Решение

используйте этот код для обновления. в поле зрения и контроллер.

 <div class="form">
<form class="cmxform form-horizontal tasi-form" id="signupForm" enctype="multipart/form-data" method="post" action="<?php echo base_url("user/update/$user->userId"); ?>">
<div class="form-group last">
<label class="control-label col-md-3">Avatar Upload:</label>
<div class="col-md-9">
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
<img src="<?php echo base_url().'upload/user/'.$user->userImg; ?> alt="" />
</div>
<div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
<div>
<span class="btn btn-white btn-file">
<span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
<span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
<input type="file" class="default" name="userImg" />
</span>
</div>
</div>
<span class="label label-danger">NOTE!</span>
<span>
Please Upload Your Profile Photo!
</span>
</div>
</div>
<div class="form-group ">
<label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class=" form-control" id="username" name="userUsername" type="text" value="<?php echo $user->userUsername; ?>" />
</div>
</div>
<div class="form-group ">
<label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="password" name="userPass" type="password" value="<?php echo $user->userPass; ?>" readonly />
</div>
</div>
<div class="form-group ">
<label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="email" name="userEmail" type="email" value="<?php echo $user->userEmail; ?>" />
</div>
</div>
<div class="form-group ">
<label for="firstname" class="control-label col-lg-2">First Name <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class=" form-control" id="firstname" name="userName" type="text" value="<?php echo $user->userName; ?>" />
</div>
</div>
<div class="form-group ">
<label for="lastname" class="control-label col-lg-2">Last Name <span class="text-danger">*</span></label>
<div class="col-lg-10">
<input class="form-control " id="lastname" name="userSurname" type="text" value="<?php echo $user->userSurname; ?>" />
</div>
</div>

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Choose Language <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userLang" class="form-control">
<option value="<?php echo $user->userLang; ?>"><?php echo $user->userLang; ?></option>
<option value="en">English</option>
<option value="ar">Arabic</option>
</select>
</div>
</div>
<!-- /basic select -->

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">User Type <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userType" class="form-control">
<option value="<?php echo $user->userType; ?>"><?php echo $user->userType; ?></option>
<option value="1">Admin = 1</option>
<option value="2">Yönetici = 2</option>
<option value="3">Departman Sorumlusu = 3</option>
<option value="4">Operatör = 4</option>
<option value="5">Stajyer = 5</option>
</select>
</div>
</div>
<!-- /basic select -->

<!-- Basic select -->
<div class="form-group">
<label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
<div class="col-lg-9">
<select name="userStatus" class="form-control">
<option value="<?php echo $user->userStatus; ?>"><?php echo $user->userStatus; ?></option>
<option value="1">Active</option>
<option value="0">Deactive</option>
</select>
</div>
</div>
<!-- /basic select -->
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit" value="Upload">Edit User</button>
<button class="btn btn-default" type="reset">Reset</button>
<button class="btn btn-danger" type="cancel">Cancel</button>
</div>
</div>
</form>
</div>

Контроллер как этот.

public function update($userId) {
if (isset($_FILES) && $_FILES['userImg']['error'] == '0') {
$config['upload_path'] = './upload/user';
$config['allowed_types']        = 'jpeg|jpg|png|gif';
$config['max_size']             = 1024;
$config['max_width']            = 1000;
$config['max_height']           = 1000;
$new_name = $this->input->post("userUsername");
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userImg'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else{
$upload_data = $this->upload->data();
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userImg" => $upload_data['file_name'],
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}
else{
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}
1

Другие решения

используйте это, если ваш URL-адрес правильный, и изображение не содержит ошибок, оно работает правильно и загрузка изображений. Я проверил это.

public function insert () {
$config['upload_path'] = './upload/user';
$config['allowed_types']        = 'jpeg|jpg|png|gif';
$config['max_size']             = 1024;
$config['max_width']            = 1000;
$config['max_height']           = 1000;
$new_name = time();
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userImg'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else{
$upload_data = $this->upload->data();
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => $this->input->post("userSurname"),
"userImg" => $upload_data['file_name'],
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$insert = $this->db->insert("user", $data);
if($insert) {
redirect(base_url("user"));
}else {

redirect(base_url("user"));

}
}
}
0

для обновления вы можете использовать этот код

public function update($userId) {
if (isset($_FILES) && $_FILES['userImg']['error'] == '0') {
$config['upload_path'] = './upload/user';
$config['allowed_types']        = 'jpeg|jpg|png|gif';
$config['max_size']             = 1024;
$config['max_width']            = 1000;
$config['max_height']           = 1000;
$new_name = $this->input->post("userUsername");
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userImg'))
{
$error = array('error' => $this->upload->display_errors());
echo json_encode($error);
}
else{
$upload_data = $this->upload->data();
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userImg" => $upload_data['file_name'],
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}
else{
$data = array (
"userUsername" => $this->input->post("userUsername"),
"userPass" => $this->input->post("userPass"),
"userEmail" => $this->input->post("userEmail"),
"userName" => $this->input->post("userName"),
"userSurname" => strtoupper($this->input->post("userSurname")),
"userLang" => $this->input->post("userLang"),
"userType" => $this->input->post("userType"),
"userDate" => date('Y-m-d H:i:s'),
);
$update = $this->db->where("userId", $userId)->update("user", $data);
if($update) {
redirect(base_url("user"));
}else {
echo "Hata!";
}
}
}
0
По вопросам рекламы [email protected]