I have a weird problem, I'm doing upload an image using codeigniter, but the thing was weird. it save to the database but part of them can upload to the specific folder, and the other not. Can someone help me ? I'm using Codeigniter 3 and PHP 7.3.13
this is my controller :
public function edit_pegawai($id_pegawai){
if (isset($_POST['simpan'])){
$this->form_validation->set_rules('nama', 'Nama', 'required');
$data_pegawai=array(
'nik'=> $this->input->post('nik'),
'nama'=> $this->input->post('nama')
);
$data['dataPegawai'] = $data_pegawai;
if ($this->form_validation->run() == TRUE){
$img = $this->UserModel->get_foto($id_pegawai)->row();
$image = $img->foto;
if ($_FILES['foto']['name']){
if ($image != NULL){
unlink('./assets/img/foto/'.$image);
}
$nama = ucwords(strtolower($this->input->post('nama')));
$config['upload_path'] = './assets/img/foto/';
$config['allowed_types'] = 'jpe|jpg|jpeg';
$config['max_size'] = 10000;
$config['file_name'] = $nama;
$config['overwrite'] = TRUE;
$this->load->library('upload');
$this->upload->initialize($config);
if ($_FILES['foto']['type'] == 'image/jpeg' || $_FILES['foto']['type'] == 'image/png' || $_FILES['foto']['type'] != 'image/jpg'){
$this->upload->do_upload('foto');
$hilihh = $this->upload->data();
$foto = $hilihh['file_name'];
}
else{
$this->session->set_flashdata('msg_gagal');
redirect(site_url('listpegawai'));
}
$this->db->update('pns_local', $data_pegawai, array('id_pegawai'=>$id_pegawai));
$this->db->set('foto', $foto);
$this->db->where('id_pegawai', $id_pegawai);
$this->db->update('pns_local');
$this->session->set_flashdata('msg_berhasil');
redirect(site_url('listpegawai'));
}
and this is my view
<form method="POST" action="<?= site_url('listpegawai/edit_pegawai/'.$dataPegawai['id_pegawai'])?>" enctype="multipart/form-data">
<input hidden type="text" name="id_pegawai" value="<?php echo $dataPegawai['id_pegawai']; ?>">
<div class="card bg-light">
<div class="form-row">
<div class="form-group col-md-4">
<label>NIK </label>
<input type="text" class="form-control" name="nik" value="<?php echo $dataPegawai['nik'];?>">
</div>
<div class="form-group col-md-4">
<label>Nama Lengkap </label>
<input type="text" class="form-control" name="nama" value="<?php echo $dataPegawai['nama'];?>">
</div>
<div class="form-row">
<div class="form-group col-md-10">
<label>Foto <span style="color: red">*</span></label><br>
<?php
if ($dataPegawai['foto'] != NULL){
?>
<img src="<?php echo base_url('assets/img/foto/'.$dataPegawai['foto'])?>" width="85" height="100">
<?php }
else{
?>
<img src="<?php echo base_url('assets/img/foto/default.png')?>" width="85" height="100">
<?php }
?>
<span> <input type="file" name="foto" accept="image/*"></span>
</div>
</div>
<button type="submit" name="simpan" class="btn btn-primary btn-block">Simpan Data
</button>
</div>
</form>
Thankyou for your help
Set $config['log_threshold'] = 4; and see the CI logs in application/logs/
Php logs can also help.
In your controller try taking ./ out of your upload path
Related
I created image upload which works fine but I also want it to be updated. When I add a need image it updates correctly but I if don't want to change the image and leave it as it is, then my current image can't be retrieve.i want to update my factory_name(field name) only sometimes. when i update image it updated successfully,but when i update factory_name only the existing image is missing. Please help me how to do this?
this my view file
<form action="<?php echo base_url('login/insert_factory/'.$id); ?>" method="POST" enctype="multipart/form-data">
<?php
$id =$this->uri->segment(3);
if($id == "")
{
?>
<div class="form-group">
<label class="form-label" for="field-1">Factory Name</label>
<!--span class="desc">e.g. "Beautiful Mind"</span-->
<div class="controls">
<input type="text" name="factory_name" class="form-control" >
</div>
</div>
<?php
}
else
{ ?>
<div class="form-group">
<label class="form-label" for="field-1">Factory Name</label>
<!--span class="desc">e.g. "Beautiful Mind"</span-->
<div class="controls">
<input type="text" name="factory_name" value="<?php echo $result->factory_name; ?>" class="form-control" >
</div>
</div>
<?php } ?>
<?php
if($id == "")
{
?>
<div class="form-group">
<label class="form-label" for="field-1">Upload Logo</label>
<!--span class="desc">e.g. "Beautiful Mind"</span-->
<div class="controls">
<input type="file" name="upload_logo" />
</div>
</div>
<?php
}
else
{ ?>
<div class="form-group">
<label class="form-label" for="field-1">Upload Logo</label>
<!--span class="desc">e.g. "Beautiful Mind"</span-->
<div class="controls">
<input type="file" name="upload_logo" value="<?php echo $result->factory_logo; ?>" />
</div>
</div>
<?php } ?>
<?php
if($id == "")
{
?>
<div class="form-group">
<div class="controls">
<input type="submit" class="btn btn-primary" value="submit" />
</div>
</div>
<?php
}
else
{ ?>
<div class="form-group">
<div class="controls">
<input type="submit" class="btn btn-primary" value="Update" />
</div>
</div>
<?php } ?>
</form>
this my controller code:
public function insert_factory()
{
$id =$this->uri->segment(3);
if($id == "")
{
if(isset($_FILES['upload_logo'])){
$errors= array();
$file_name = $_FILES['upload_logo']['name'];
$file_tmp =$_FILES['upload_logo']['tmp_name'];
if(empty($errors)==true){
move_uploaded_file($file_tmp,"assets/uploads/".$file_name);
// echo "Success";
}else{
print_r($errors);
}
$fields = array(
'factory_name' => $this->input->post('factory_name',True),
'factory_logo' => $file_name
);
$result = $this->login_model->insert_factory($fields);
if($result){
$this->session->set_flashdata('success_msg',' Factory Created Successfully!');
redirect('login/view_factory');
}else{
$this->session->set_flashdata('error_msg','Factory Cannot Created.!');
redirect('login/create_factory');
}
}
}else{
if(isset($_FILES['upload_logo'])){
$errors= array();
$file_name = $_FILES['upload_logo']['name'];
$file_tmp =$_FILES['upload_logo']['tmp_name'];
if(empty($errors)==true){
move_uploaded_file($file_tmp,"assets/uploads/".$file_name);
// echo "Success";
}else{
print_r($errors);
}
$fields = array(
'factory_name' => $this->input->post('factory_name',True),
'factory_logo' => $file_name
);
$this->db->where('factory_id', $id);
$this->db->update('create_factory', $fields);
if($this->db->affected_rows()>0)
{
$this->session->set_flashdata('success_msg',' Factory Updated Successfully!');
}else{
$this->session->set_flashdata('error_msg','Factory Cannot Updated.!');
redirect('login/create_factory');
}
redirect(base_url('login/view_factory'));
}
}
}
And this my model file:
public function insert_factory($fields)
{
$this->db->insert('create_factory',$fields);
return ($this->db->affected_rows() != 1) ? false : true;
}
In your controller, in both if and else blocks, please modify your existing code in order to not update empty image when image is not provided and only factory name is provided.
This code in both if and else blocks:
$fields = array(
'factory_name' => $this->input->post('factory_name',True),
'factory_logo' => $file_name
);
Needs to be change to:
$fields['factory_name'] = $this->input->post('factory_name', True);
if (!empty($file_name)) {
$fields['factory_logo'] = $file_name;
}
I have 2 websites in 1 htdocs directory. The structure's like this
The main directory it's structured like this(name:Quartiere):
Quartiere
_admin
application
assets
Second website directory it's _admin with the same structure(default CodeIgniter structure).
I'm trying to upload a file from _admin to the main directory assets folder, but for some reason, it's not working.I tried other answers from other post but nothing it helped.
What I tried:
Put 'userfile' in do_upload
Checked if the form it's multipart(it is)
What I checked:
If the path it's good(it is)
If the size of the photo it's smaller than 2048(it is)
_admin Controller:
public function add_product(){
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Adauga Produs';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('name','Nume','required');
$this->form_validation->set_rules('category','Categorie','required');
$this->form_validation->set_rules('ingredients','Ingrediente');
$this->form_validation->set_rules('price','Pret','required');
$this->form_validation->set_rules('grams','Gramaj','required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('products/adauga', $data);
$this->load->view('templates/footer');
}else{
//upload image
$config['upload_path'] = 'http://localhost/quartiere/assets/images';
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error' => $this->upload->display_errors());
$food_image = 'noimage.png';
}else{
$data = array('upload_data' => $this->upload->data());
$food_image = $_FILES['userfile']['name'];
}
$this->food_model->add_product($food_image);
redirect('produse/adauga');
}
}
_admin View:
<?php echo validation_errors();?>
<?php echo form_open_multipart('products/add_product');?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1 class="text-center">
<?php echo $title; ?>
</h1>
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Nume Produs" required/>
</div>
<div class="form-group">
<select class="form-control" name="category" required>
<?php foreach($categories as $category):?>
<option value="<?php echo $category['id'];?>"><?php echo $category['category_name'];?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Bar:</label><br>
Da <input type="radio" name="bar" class="from-control" value="1"/>
Nu <input type="radio" name="bar" class="from-control" value="0"/>
</div>
<div class="form-group">
<input type="text" name="ingredients" class="form-control" placeholder="Ingrediente"/>
</div>
<div class="form-group">
<input type="number" name="grams" class="form-control" placeholder="Gramaj"/>
</div>
<div class="form-group">
<label>Poza:</label>
<input type="file" name="userfile" size="10"/>
</div>
<div class="form-group">
<input type="number" name="price" class="form-control" placeholder="Pret" required/>
</div>
<button type="submit" class="btn btn-success btn-block">Adauga Produs</button>
</div>
</div>
<?php form_close(); ?>
You can make the upload path to - '../assets/images' - or - $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images'. Either way is fine.
I have two fields one for link and one for file upload in form, how do i add only one of those i.e. either link or file in database,
Sorry for any wrong code and methods, I am a newbie here and this is my first question in stack overflow. Hope you all help me out.
My Controller is
public function add_scrolllink()
{
$this->form_validation->set_rules('stitle','File Title','trim|required');
$this->form_validation->set_rules('slink','File Link','trim|required');
if(empty($_FILES['sfile']['name']))
{
$this->form_validation->set_rules('slink','File Link','required');
}
if($this->form_validation->run() == FALSE)
{
$this->load->view("add_scrolllink");
}
else
{
$slink_id = $this->input->post('stitle');
$slink_files = $this->input->post('slink');
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'pdf|docx|doc';
$config['max_size'] = 9000;
$this->upload->initialize($config);
$this->load->library('upload',$config);
if(!$this->upload->do_upload('slink'))
{
$msg1 = $this->upload->display_errors();
header("Location:".base_url()."news/add_scrolllink?msg1=$msg1");
}
else
{
$output = $this->upload->data();
$file2 = $output['file_name'];
$this->load->model('mpages');
$output1 = $this->mpages->add_scrolllink($slink_title,$file2);
if($output1)
{
$msg = "Scrolling Link added successfully!";
header("Location:".base_url()."news/scrolllink_list?
msg=$msg");
}
else
{
$msg1 = 'Scrolling Link add failed. Try again!';
header("Location:".base_url()."news/add_scrolllink?
msg1=$msg1");
}
}
}
//My View is
<form action="<?php echo base_url(); ?>news/add_scrolllink" method="post"
role="form" enctype="multipart/form-data">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
<label>Scrolling Link Title</label>
<input type="text" name="stitle" class="form-
control" value="<?php echo set_value('stitle'); ?>" placeholder="Enter
Scrolling Link Title">
</div>
<style type="text/css">
p.help-block{color:#ff0000;}
</style>
<div class="form-group">
<label>Scrolling Link</label>
<input type="text" name="slink" class="form-control"
value="<?php echo set_value('slink'); ?>" placeholder="Enter Scrolling
Link">
</div>
OR ADD FILE TO SCROLL LINK
<div class="form-group">
<label>Choose File to Upload</label>
<input type="file" name="slink" class="form-
control">
</div>
<input type="submit" name="submit" class="btn btn-
primary" value="Add Link"><br>
<?php echo "<br><span
style='color:#ff0000;'>".validation_errors()."</span>"; ?>
<?php
if(isset($_GET['msg'])){
echo "<div class='alert alert-success
fadein'>".$_GET['msg']."</div>";
}
if(isset($_GET['msg1'])){
echo "<div class='alert alert-danger
fadein'>".$_GET['msg1']."</div>";
}
?>
</div>
</div>
</div>
<div class="col-md-2"></div>
<div class="clearfix"></div>
</div>
</form>
you have use jquery to get input box value using this code and set some condition
$('#slink-id').val(); // set link field input box id
$('#slinkFile-id').val(); //set upload file field id
if($('#slink-id').val() != '' && $('#slinkFile-id').val() == ''){
// setread only your file field
}else{
// set read only link field
}
I am trying to upload the three images from html form,with three different names.But it shows error
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->input->post('mobile_name').'_front.jpg';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload('files'))
{
$_POST['mobile_img1']= $this->upload->data()['file_name'];
}
else
{
echo "image upload fails";
exit();
}
This my codeigniter code to upload one image.
I have three image input field in html code
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="form-group">
<label class="control-label">Mobile Image Front</label>
<input type="file" name="mobile_img1" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Back</label>
<input type="file" name="mobile_img2" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Side</label>
<input type="file" name="mobile_img3" class="filestyle" data-placeholder="No file">
</div>
</div>
</div>
in this html field i need to upload the first input as mobilename_front.jpg , second as mobilename_back.jpg and third as mobilename_side.jpg and also i need to upload this names in db.Give some ideas to implement this,
View Files
<div class="row">
<div class="col-md-4 col-md-offset-4">
<form method="POST" action="<?php echo site_url('my-controller/file_upload');?>" enctype='multipart/form-data'>
<div class="form-group">
<label class="control-label">Mobile Image Front</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Back</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<div class="form-group">
<label class="control-label">Mobile Image Side</label>
<input type="file" name="mobile_img[]" class="filestyle" data-placeholder="No file">
</div>
<input type="submit" value="upload"></form>
</div>
</div>
My controller:
<?php
class My_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
}
public function file_upload(){
$files = $_FILES;
$cpt = count($_FILES['mobile_img']['name']);
for($i=0; $i<$cpt; $i++)
{
$mobileView=(($i == 0)?"_front":(($i == 1)?"_back":"_side")); //Here i assign first image(i = 0) as _front, second image(i = 1) as _back,third image(i = 2) as _side
$_FILES['mobile_img']['name']= $files['mobile_img']['name'][$i].$mobileView.time();// time() for time in seconds becauze we may need if same name uploaded file name
$_FILES['mobile_img']['type']= $files['mobile_img']['type'][$i];
$_FILES['mobile_img']['tmp_name']= $files['mobile_img']['tmp_name'][$i];
$_FILES['mobile_img']['error']= $files['mobile_img']['error'][$i];
$_FILES['mobile_img']['size']= $files['mobile_img']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['mobile_img']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);
$this->my_model->upload_image($fileName);
}
private function set_upload_options()
{
// upload an image options
$config = array();
$config['upload_path'] = './upload/'; //give the path to upload the image in folder
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
}
?>
My model Function:
<?php
class My_Model extends CI_Model {
public function upload_image($fileName)
{
if($filename!='' ){
$filename1 = explode(',',$filename);
foreach($filename1 as $file){
$file_data = array(
'name' => $file,
'datetime' => date('Y-m-d h:i:s')
);
$this->db->insert('uploaded_files', $file_data);
}
}
}
}
?>
<?php echo form_open('admin/product_insert');?>
<div class="row form-group">
<div class="col-lg-2">
Image 1
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo1" required>
</div>
</div>
<div class="row form-group">
<div class="col-lg-2">
Image 2
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo2" Required>
</div>
</div>
<div class="row form-group">
<div class="col-lg-2">
Image 3
</div>
<div class="col-lg-5">
<input type="file" class="form-control" name="photo3" Required>
</div>
</div>
<input type="submit">
public function product_insert(){
$pro=$this->input->post('productName');
for($i=0;$i<=3;$i++){
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name']=$pro.$i;
$this->load->library('upload',$config);
$this->upload->do_upload('photo'.$i);
$this->upload->initialize($config);
}
controller
<?php
class Breaking extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('breaking_model');
}
public function create_breaking_news()
{
$this->securePage();
if ($this->input->post('submit')) {
$this->form_validation->set_rules('title', ' Breaking News Title', 'required');
$this->form_validation->set_rules('details', ' Breaking News Details', 'required');
if ($this->form_validation->run()) {
$added_on = time();
// if image is uploaded
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['size']>0) {
$config['upload_path'] = $this->config->item('upload_path');
$config['allowed_types'] = $this->config->item('allowed_types');
$config['max_size'] = $this->config->item('max_size');
$config['max_width'] = $this->config->item('max_width');
$config['max_height'] = $this->config->item('max_height');
$config['encrypt_name'] = $this->config->item('encrypt_name');
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$image = $this->upload->data();
$this->breaking_model->add_news($image['file_name'], $added_on);
$this->session->set_flashdata('msg', '<button class="btn btn-success" style="width:100%;">The Breaking News has been added successfully.</button>');
redirect('breaking/manage-breaking-news');
} else {
$this->session->set_flashdata('msg', $this->upload->display_errors());
redirect('breaking/create-breaking-news');
}
} else {
// if image is not uploaded
$this->breaking_model->add_news(null, $added_on);
$this->session->set_flashdata('msg', '<button class="btn btn-success" style="width:100%;">The About Breaking News been added successfully.</button>');
redirect('breaking/manage-breaking-news');
}
}
}
$data['news_title'] = 'Add Breaking news | Kanchan news.com';
$data['Keywords'] = '';
$data['url'] = '';
$data['content'] = $this->load->view('breaking/create', $data, true);
$this->load->view('kanchan', $data);
}
}
my models
function add_news($image, $added_on) {
$this->db->set('title', $this->input->post('title'));
$this->db->set('details', $this->input->post('details'));
$this->db->set('video', $this->input->post('video'));
$this->db->set('images', $image);
$this->db->set('added_on', $added_on);
$this->db->insert('breaking_news');
}
and my view
<div class="container">
<ol class="breadcrumb">
<li class="active"><?php echo $this->session->userdata('full_name');?></li>
<li class="active">Breaking News</li>
<li class="active">Add Breaking News</li>
</ol>
<?php echo $this->session->set_flashdata('msg');?>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title"> Enetr Breaking News Title :</label>
<hr>
<input type="text" name="title" class="form-control" value="<?php echo set_value('title');?>">
<?php echo form_error('title');?>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="title"> Enetr Breaking News Images :</label>
<hr>
<input type="file" name="userfile">
<?php echo form_error('userfile');?>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="title"> Enetr Breaking News Video Url( not required) :</label>
<hr>
<input type="video" name="video" class="form-control" value="<?php echo set_value('video');?>">
<?php echo form_error('video');?>
</div>
</div>
</div>
<div class="form-group">
<label for="title"> Enetr Breaking News Details :</label>
<hr>
<textarea class="ckeditor" name="details"><?php echo set_value('details');?></textarea>
<?php echo form_error('details');?>
</div>
<button class="btn btn-success" name="submit" value="submit" type="submit">Add Breaking News</button>
</form>
<hr>
</div>
i can add data without image successfully but when i select image it wont added data and redirect too create page without any error message and same problems with update too