I cannot seem to upload files in codeigniter. I don’t know if the issue lies with the if ($_FILES[‘avatar’][‘name’] == “”).
My controller
My controller
private function upload_avatar($file)
{
$newName = $file->getRandomName();
$upload = $file->move(ROOTPATH . 'public/assets/avatar', $newName);
if ($upload) {
return $newName;
} else {
return false;
}
}
public function change_data()
{
helper(['form', 'url']);
$userModel = new UserModel();
if ($this->request->getMethod() == 'post') {
if ($_FILES['avatar']['name'] == "")
{
$rules = [
'nama' => 'required|alpha_space|min_length[2]',
'email' => 'required|valid_email',
'nip' => 'required|min_length[2]',
'tempat_lahir' => 'required|alpha_space|min_length[2]'
];
} else {
$rules = [
'nama' => 'required|alpha_space|min_length[2]',
'email' => 'required|valid_email',
'nip' => 'required|min_length[2]',
'tempat_lahir' => 'required|alpha_space|min_length[2]',
'avatar' => [
'uploaded[avatar]',
'mime_in[avatar,image/jpg,image/jpeg,image/png]',
'max_size[avatar,4096]'
]
];
}
if ($this->validate($rules)) {
if ($_FILES['avatar']['name'] == "") {
$params = [
'nama' => $userModel->escapeString(esc($this->request->getPost('nama'))),
'email' => $userModel->escapeString(esc($this->request->getPost('email'))),
'nip' => $userModel->escapeString(esc($this->request->getPost('nip'))),
'tempat_lahir' => $userModel->escapeString(esc($this->request->getPost('tempat_lahir'))),
];
} else {
//get data user by session email
$user = $userModel->where('email', session()->get('email'))
->first();
if ($user) {
$deleteFile = unlink('./assets/avatar/' . $$user['avatar']);
if ($deleteFile) {
$file = $this->request->getFile('avatar');
$uploadFile = $this->upload_avatar($file);
}
}
$params = [
'nama' => $userModel->escapeString(esc($this->request->getPost('nama'))),
'email' => $userModel->escapeString(esc($this->request->getPost('email'))),
'nip' => $userModel->escapeString(esc($this->request->getPost('nip'))),
'tempat_lahir' => $userModel->escapeString(esc($this->request->getPost('tempat_lahir'))),
'avatar' => $uploadFile,
];
}
$update = $userModel->update($user['id_user'], $params);
if ($update) {
session()->setFlashdata('success', 'Berhasil Update Data. Apabila Tampilan Data Belum Berubah, Silakan Lakukan Logout dan Login Kembali');
return redirect()->route('profile');
} else {
session()->setFlashdata('danger', 'Gagal Update Data');
return redirect()->route('edit')->withInput();
}
} else {
$data['validation'] = $this->validator;
}
}
$data['title'] = 'Edit Profile';
return view('admin/users/ubah_data', $data);
}
My view
<form action="<?= base_url('admin/user/change_data') ?>" method="POST">
<?= csrf_field(); ?>
<div class="form-group">
<label for="nama">Nama</label>
<input type="text" class="form-control" id="nama" name="nama" value="<?= session()->nama ?>">
</div>
<div class="form-group">
<label for="nip">NIP</label>
<input type="text" class="form-control" id="nip" name="nip" value="<?= session()->nip ?>">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email" value="<?= session()->email ?>">
</div>
<div class="form-group">
<label for="tempat_lahir">Tempat Lahir</label>
<input type="text" class="form-control" id="tempat_lahir" name="tempat_lahir" value="<?= session()->tempat_lahir ?>">
</div>
<div class="form-group">
<label for="avatar">Foto <small>(Optional)</small></label>
<div class="custom-file">
<input type="file" class="custom-file-input" id="avatar" name="avatar">
<label class="custom-file-label" for="avatar">Choose file</label>
</div>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info" />
</div>
</form>
After i push the upload button Undefined index: avatar message appeared. Any help will be greatly appreciated. I cannot seem to figure out why ($_FILES[‘avatar’][‘name’] == “”) has problem
It seems like the issue is with the line if ($_FILES['avatar']['name'] == "")
. This condition is checking if the ‘avatar’ file input field is empty, but it can only be checked after the form is submitted, and when you try to access the ‘avatar’ input in the form, it throws an “Undefined index: avatar” error.
To fix this issue, you should check if the ‘avatar’ input exists in the form after the form submission using the isset()
function. Here’s how you can modify your code:
if ($this->request->getMethod() == ‘post’) {
$rules = [
‘nama’ => ‘required|alpha_space|min_length[2]’,
’email’ => ‘required|valid_email’,
‘nip’ => ‘required|min_length[2]’,
‘tempat_lahir’ => ‘required|alpha_space|min_length[2]’,
];if ($this->request->getFile(‘avatar’)) {
// If the ‘avatar’ input exists in the form
$rules[‘avatar’] = [
‘uploaded[avatar]’,
‘mime_in[avatar,image/jpg,image/jpeg,image/png]’,
‘max_size[avatar,4096]’
];
}// Rest of your code…
}
By using $this->request->getFile('avatar')
, you can check if the ‘avatar’ input exists in the form, and if it does, you can add the corresponding validation rules.
Additionally, you can use the hasFile()
method to check if the file exists before moving it. Here’s how you can update your upload_avatar
method:
private function upload_avatar($file)
{
if ($file->isValid() && !$file->hasMoved()) {
$newName = $file->getRandomName();
if ($file->move(ROOTPATH . ‘public/assets/avatar’, $newName)) {
return $newName;
} else {
return false;
}
}
return false;
}
With these modifications, you should be able to handle the file upload without encountering the “Undefined index: avatar” error. Remember to add further validation and security measures as needed