so i try to make some upload form which will upload some image. but when the image has chosen then the button submit has clicked the result say "You did not select a file to upload."
im using php, with codeigniter framework
The view
<form class="text-left" enctype="multipart/form-data" method="post" action="<?= base_url('aduan/add'); ?>">
<div class="form-group">
<label>Upload Foto Bukti Aduan</label>
<div class="form-group">
<label>Bukti Aduan 1</label>
<input class="form-control-file" type="file" id="bukti1" name="bukti_aduan1" />
<?= form_error('bukti_aduan1', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
<div class="form-group">
<label>Bukti Aduan Pelapor di Lokasi</label>
<input class="form-control-file" type="file" id="bukti2" name="bukti_aduan2" />
<?= form_error('bukti_aduan2', '<p class="text-danger font-weight-bold pl-2">', '</p>'); ?>
</div>
</div>
which the view will run the controller "aduan" for input function
Controller file:
$aduan = $this->aduan_model;
$validation = $this->form_validation;
$validation->set_rules($aduan->rules());
if ($validation->run()) {
$aduan->tambah();
$this->session->set_flashdata('success', 'Berhasil disimpan');
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/aduansukses');
$this->load->view('home/home_footer');
} else {
$data['aduan'] = $this->aduan_model->getAll();
$data['title'] = 'Buat Aduan Kasus Lingkungan';
$this->load->view('home/home_header', $data);
$this->load->view('home/buataduan');
$this->load->view('home/home_footer');
}
then the controller will load a model which is "aduan_model"
Model file :
public function tambah()
{
$post = $this->input->post();
$this->nik_pelapor = $post["nik_pelapor"];
$this->nama_pelapor = $post["nama_pelapor"];
$this->email_pelapor = $post["email_pelapor"];
$this->telp_pelapor = $post["telp_pelapor"];
$this->alamat_pelapor = $post["alamat_pelapor"];
$this->aduan = $post["aduan"];
$this->bukti_aduan1 = $this->_uploadImage();
$this->bukti_aduan2 = $this->_uploadImage2();
$this->status_aduan = 'Dalam proses';
$this->tgl_aduan = time();
$this->db->insert($this->_table, $this);
}
private function _uploadImage()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan1')) {
return $this->upload->data();
}
return "default.jpg";
}
private function _uploadImage2()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
// $config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('bukti_aduan2')) {
return $this->upload->data();
}
return "default.jpg";
}
i expect the output of image can be stored in file upload and inserted in database but it doesnt select any file
please help :)
try my code
replace if condition in controller to upload
if(
(!$this->upload->do_upload('bukti_aduan2')) &&
(! $this->upload->do_upload('bukti_aduan1'))
)
{
echo "failed";
}
else
{
echo "success";
}
Related
view :
<form action="#" enctype="multipart/form-data" id="form" class="form-horizontal">
<div class="form-group">
<label class="control-label col-md-3" id="label-photo">Upload Photo </label>
<div class="col-md-9">
<input name="photo" type="file">
<span class="help-block"></span>
</div>
</div>
Controller :
private function _do_upload()
{
/* $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
// */
$config['upload_path'] = 'uploads/';
// $config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['allowed_types'] = 'jpg|gif|png|jpeg|JPG|PNG';
$config['max_size'] = 5000; //set max size allowed in Kilobyte
$config['max_width'] = 3000; // set max width image allowed
$config['max_height'] = 3000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if(!$this->upload->do_upload('photo')) //upload and validate
{
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
When I am trying to upload an image getting this error and I cann't find any solution. Tried by changing upload path and allowed types.
Ok so, You try this Code.You can upload image successfully.
View File : uoload.php
<form action="upload_controller" method="post" enctype="multipart/form-data">
First name: <input type="file" name="photo"><br>
<input type="submit" value="Submit">
</form>
Now This Is my Upload Controller :
public function upload_controller(){
if (count($_FILES) > 0) {
if (count($_FILES['photo']) > 0) {
if ($_FILES['photo']['name'] != '' && $_FILES['photo']['size'] > 0) {
$filename = $_FILES['photo']['name'];
$filesize = $_FILES['photo']['size'];
$config['upload_path'] = '../admin_zency/data/doctors/'; // Your Upload Path Directory
$config['allowed_types'] = '*';
$config['max_size'] = $this->config->item('image_size');
$config['max_width'] = $this->config->item('user_width');
$config['max_height'] = $this->config->item('user_height');
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('photo');
$imagedata = $this->upload->data();
$imagearror = $this->upload->display_errors();
}
}
}
}
Thanks and give ratting if this code is helpfull to you.
I can't upload an image file, I don't know the input file and can't detect it. That's why I can't upload a file.
This is the controller, I named it desain.php.
defined('BASEPATH') OR exit('No direct script access allowed');
class Desain extends CI_Controller {
public function index()
{
$query = "select * from result,kategori where result.id_cat=kategori.id_cat";
$tampil = $this->db->query($query)->result();
$this->load->view('Admin/data.php', ['tampil' => $tampil]);
}
public function do_upload()
{
$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048000';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['file_name'] = $this->input->post('photo');
$this->load->library('upload',$config);
if(!$this->upload->do_upload('photo'))
{
$error = array ('error' =>$this->upload->display_errors());
redirect('admin/desain');
}
else { $data = $this->upload->data($config);
$data = array('photo' =>$this->upload->data($config),
'nama_desain' =>$this->input->post('nama'),
'id_cat' =>$this->input->post('categori'),
'ket' =>$this->input->post('ket'),
);
$this->db->insert('result',$data);
redirect('admin/desain');
}
}
}
This is the view:
<form role="form" action="<?php echo base_url();?>admin/desain/do_upload" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="exampleInputEmail1">Nama Desain</label>
<input type="text" class="form-control" name ="nama" id="nama" placeholder="Nama Desain">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Kategori</label>
<select class="form-control" id="categori" name="categori">
<option value="">Pilih Kategori</option>
<option value="1">Vektor</option>
<option value="2">Editing</option>
<option value="3">Desain</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Keterangan Gambar</label>
<textarea class="form-control" rows="3" name="ket" id="ket" placeholder="Enter ..."></textarea>
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" required="required" id="photo" name="photo" class="form-control">
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
I tried to post this form, but this file input is NULL.
Try this hope it will help you
$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048000;
$config['max_width'] = 1024;
$config['max_height'] = 1024;
$config['file_name'] = $this->input->post('photo');
$this->load->library('upload', $config);
if (!$this->upload->do_upload($config['file_name']))
{
$error = $this->upload->display_errors();
//coding...
}
else
{
$filename = $this->upload->data();
$data[$config['file_name']] = $filename['file_name'];
//coding...
}
You should catch the file by
$config['file_name'] = $_FILES['name']['photo'];
Than you will be able to get the image file to process.
Before it please check if the field is able to get or not by
if(!empty($_FILES['name']['photo']))
This was perfectly working on my computer. Hope it will be helpful for you.
You don't need to do this: $config['file_name'] = $this->input->post('photo');
By default the upload class will use the name of the image as the name of the final image. If you wanted to do it this way than $_FILES['photo']['name']; would work (but again, there is no need).
Further you aren't getting the value of the uploaded photo correctly. I'm not sure why you are putting the config array in this function: $this->upload->data($config); but that is not how it supposed to be used.
If you wanted to get the filename for example you could do:
$filename = $this->upload->data('file_name');
OR
$up_data = $this->upload->data();
$filename = $up_data['file_name']
See this for reference: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data
Also you are not doing anything with this line: $error = array ('error' =>$this->upload->display_errors());. I suggest setting the error as a flash message before redirecting.
if(!$this->upload->do_upload('photo'))
{
$error = array ('error' =>$this->upload->display_errors());
redirect('admin/desain');
} else {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$data = array(
'photo' => $uploadData['file_path'].'upload_images/'. $picture,
'nama_desain' =>$this->input->post('nama'),
'id_cat' =>$this->input->post('categori'),
'ket' =>$this->input->post('ket'),
);
$this->db->insert('result',$data);
redirect('admin/desain');
}
I'm stuck from make a update image from codeigniter, i want replace image when i update it but, the code its doesn't work.
here is my code :
this is Controller :
public function updatefoto(){
$this->load->model('model_users');
$this->model_users->updatefoto();
}
This is The Model
public function updatefoto($userId){
if($this->input->post('submit')){
$config['upload_path'] = "./uploads/images/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$config['file_name'] = 'gambar-'.trim(str_replace(" ","",date('dmYHis')));
$this->load->library('upload', $config);
if (!$this->upload->do_upload("gambar")) {
$error = array('error' => $this->upload->display_errors('<div class="alert alert-danger">×','</div>'));
$this->load->view('view_ubah_data-profile', $error);
}else{
$nama=$this->upload->data('file_name');
$this->db->where('id', $userId);
$this->db->update('foto',array('nama_foto'=>$nama));
redirect('view_ubah_data-profile','refresh');
}
}
}
and the last is view
<div class="col-md-4 col-sm-6 col-xs-12">
<form action="users/updatefoto" method="post" id="updateUserImage">
<div class="text-center">
<img src="<?php echo base_url();?>uploads/images/<?php echo $userData['nama_foto'] ?>" cclass="img-thumbnail" alt="avatar">
<h6>Upload a different photo...</h6>
<input type="file" class="text-center center-block well well-sm" name="gambar" id="files" accept="image/*" required>
<button type="submit" value="Upload" name="submit" class="btn btn-primary">
<i class="fa fa-upload" aria-hidden="true"></i>upload
</button>
</div>
</form>
</div>
Thanks I appreciate any help :)
Here is how I do it.
Edit- Class Construct
class Team extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('team_model');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile1'))
{
$data['errors'] = array('error' => $this->upload->display_errors());
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// Here I load my Views
}
else
{
$upload_data = $this->upload->data();
$file_name=$upload_data['file_name'];
$email=$this->session->userdata['email'];
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// If there is no image insert one other wise update
if($data['pic']['image']!=''){
$this->team_model->updateEmployeeImage($data['pic']['id'],$file_name);
$data['success']='Congratulations! Image Updated Successfully';
}else{
$this->team_model->InsertEmployeeImage($data['client_Id']['id'],$file_name);
$data = array('upload_data' => $this->upload->data());
$data['success']='Congratulations! Image Uploaded Successfully';
}
$file_name=$upload_data['file_name'];
$email=$this->session->userdata['email'];
$data['client_Id']=$this->team_model->getUserId($email);
$data['pic']=$this->team_model->getImage($data['client_Id']['id']);
// Here I load my Views
}
}
}
Edit- Model Functions
public function InsertEmployeeImage($id,$image){
$Image=array(
'id' => '',
'team_id' => $id,
'image' => $image
);
$this->db->insert('teams_image',$Image);
return ;
}
public function updateEmployeeImage($id,$img){
$this->deleteOldImage($id);
$pic=array(
'image' => $img
);
$this->db->WHERE('id',$id)->update('teams_image',$pic);
}
Problem is When upload a image its shows same error every time "The upload path does not appear to be valid". Tried so much solution but still same problem with it..Please help.
Controller:
public function updateprofile()
{
/***get current user data***/
$this->load->model('Admin_main','userinfo');
$data=$this->userinfo->getuserdata();
$this->load->model('Admin_main','updateprofile');
$result=$this->updateprofile->updatemyprofile();
if($data->identity=='admin')
{
redirect('admin/myprofile');
}
else
{
redirect('campuser/myprofile');
}
}
Model:
if($_FILES['fileToUpload']['name']!='')
{
$config["upload_path"] = './uploads/';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$mypic = array('profile_pic'=>$picture);
$this->db->set($mypic);
$this->db->where('id', $userid);
$uppic=$this->db->update('mokhayam_users');
}
else
{
echo $this->upload->display_errors();
}
}
View:
<form class="form-horizontal" action="admin/updateprofile" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">Profile Picture</label>
<div class="col-sm-10">
<label for="fileToUpload"><img src="assets/admin/dist/img/gul.jpg" id="blah1" class="profile-user-img img-responsive img-circle"/></label><input type="file" name="fileToUpload" id="fileToUpload" onchange="$('#blah1')[0].src = window.URL.createObjectURL(this.files[0])">
</div>
</div>
<input type="hidden" name="userid" value="2">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Thanks in advance..
If your codeigniter version is 3.x please change library file (system/library/upload.php) version codeigniter 3.x to 2.x
and then try
$config["upload_path"] = APPPATH.'/uploads/';
......
Without changing anything in your library, the solution is to initialize the config just after loading the upload library in your controller, follow this code
// Upload Image
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '7048';
$config['max_width'] = '7000';
$config['max_height'] = '7000';
// Create folder (Uploads) if not exists
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path']);
}
// Load library Upload
$this->load->library('upload', $config);
// Initialize the Upload library with current $config
$this->upload->initialize($config);
$this->upload->do_upload();
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
$page_image = $_FILES['userfile']['name'];
} else {
$errors = array('error' => $this->upload->display_errors());
$page_image = 'noimage.jpg';
}
// Set message
$this->session->set_flashdata('page_created', 'Your File(s) has been uploaded');
I would like to know with my code how am I able to make my file upload required. Because codeigniter form validation set rules is for post only.
I am unsure on how to make the file upload a requirement on form submit so it checks if file is required. And lets me submit form if file is upload or something along those lines
The callback works fine but required function cannot get to work with file upload.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Store_add extends MX_Controller {
public function __construct() {
parent::__construct();
if (!$this->session->userdata('isLogged')) {
redirect('admin');
}
$this->load->model('admin/setting/model_store_add');
$this->load->model('admin/setting/model_store_get');
$this->lang->load('admin/setting/store_add', $this->settings->get('config_admin_language'));
}
public function index() {
// Title Language
$this->document->setTitle($this->lang->line('heading_title'));
$this->load->library('form_validation');
$this->form_validation->set_rules('config_image', 'Store Image', 'callback_do_upload_image');
if ($this->form_validation->run($this) == FALSE) { // "$this is for HMVC MY_Form_validation"
return $this->load->view('setting/store_add.tpl');
} else {
$store_id = $this->model_store_add->add_store();
$this->do_upload_image($store_id);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/setting/store');
}
}
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('config_image')) {
$this->load->library('form_validation');
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
}
}
Sample View form
<div class="panel-body">
<?php $data = array('id' => 'form-store-add', 'role' => 'form', 'class' => 'form-horizontal' );?>
<?php echo form_open_multipart('admin/setting/store/add', $data);?>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $this->load->view('setting/store_flashdata.tpl');?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"><?php echo $entry_image; ?></label>
<div class="col-sm-10">
<input type="hidden" name="config_image" value="<?php echo set_value('config_image', '');?>">
<input type="file" name="config_image" size="20"/>
</div>
</div>
<?php echo form_close();?>
</div>
WORKING NOW: I did more research on http://php.net/ I have fixed form validation for file upload made sure it is required I added is_file_upload and also added set form validation message with a return of false
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (is_uploaded_file($_FILES['config_image']['tmp_name'])) {
if (!$this->upload->do_upload('config_image')) {
$this->load->library('form_validation');
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
} else {
$this->form_validation->set_message('do_upload_image', 'Image Required');
return false;
}
}