Php codeigniter is not uploading an image - php

This is my view code of uploading an image but it is showing error You did not select a file to upload.
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post">
<div class="form-row">
<input type="text" required="" name="speciality_Name" placeholder="Speciality Name" >
<?php echo form_error('speciality_Name','<span class="help-block">','</span>'); ?>
</div>
<input type="file" name="image" size="20"/>
<?php echo form_error('image','<span class="help-block">','</span>'); ?>
<div class="form-row">
<input type="submit" name="addSubmit" value="Add Speciality"/>
</div>
</form>
This is code of my controller dashboard and function is saveSpeciality. when i try to upload image it shows error "You did not select a file to upload.".
public function saveSpeciality()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '2048000';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$config['overwrite'] = true;
$this->load->library('upload', $config);
$imag="image";
if( ! $this->upload->do_upload($imag)){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
echo $this->upload->file_name;
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = array('upload_data' => $this->upload->data());
}
}

You can try add enctype="multipart/form-data" to your <form>

add enctype="multipart/form-data" attribute to your form
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post" enctype="multipart/form-data">
</form>

Try This
<form id="signupForm" action="<?php echo base_url();?>dashboard/saveSpeciality" method="post" enctype="multipart/form-data">
$file_name = time() . "." . pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
$config['max_size'] = '2048000';
$config['max_width'] = '2048';
$config['max_height'] = '2048';
$config['file_name'] = $file_name;
$config['overwrite'] = true;
$obj->upload->initialize($config);
$imag = "image";
if (!$obj->upload->do_upload($imag)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
//return $obj->upload->display_errors();
} else {
$data = array('upload_data' => $this->upload->data());
}

Related

Codeigniter 3 : inputerror : The filetype you are attempting to upload is not allowed.

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.

codeigniter upload path does not appear to be valid

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');

CodeIgniter Upload Image no input

I am trying to implement CodeIgniter's upload library. But everytime I tried to upload, it keeps on saying that I haven't selected a file to upload.
Here is the code:
View:
<form action="<?= base_url().'principal/updateuniform' ?>" method="post">
<h3 style="text-align:center">Polo</h3>
<input type="hidden" name="imageid" value="<?= $img_id ?>">
<input type="hidden" name="imagename" value="<?= $img_name ?>">
<input type="file" name="imagefile" accept="image/*" /><br>
<button type="submit" class="btn btn-gold"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Upload</button>
</form>
Upload function:
function updateuniform(){
$img_id = $this->input->post('imageid');
$img_name = $this->input->post('imagename');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5000;
$config['max_width'] = 3000;
$config['max_height'] = 4000;
$config['overwrite'] = TRUE;
$config['file_name'] = $img_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = $this->input->post('imagefile');;
$id = $this->session->userdata('id');
$user['user'] = $this->UsersModel->select_principal($id);
if (!$this->upload->do_upload($img)){
$data['error'] = $this->upload->display_errors();
$data['uniforms'] = $this->InfoModel->getUniforms();
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
} else {
$img_path = 'images/'.$this->upload->data('orig_name');
$data['success'] = 'Uniforms has been updated.';
$this->InfoModel->updateUniform($img_path);
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
}
}
I also tried to echo the $img and it's displaying the file name.
$this->upload->do_upload($img)
The param should be the filedname,like:
$this->upload->do_upload('imagefile')
Add enctype="multipart/form-data" in your form tag of view this allows you to get $_FILE value and This will solve your issue I guess

Getting "The filetype you are attempting to upload is not allowed." in codeigniter while uploading video

I am stuck here.. I was trying to upload image and video, image upload working fine but while uploading video getting the error as "The filetype you are attempting to upload is not allowed.". I have tried all the answers but unfortunately couldn't workout. Can someone plz help me. What Am I doing wrong? Thanks.
My View 'home':
<div style="margin-right: 17px;">
<label>Name:</label>
<input type="text" name="name" value="<?php echo set_value('name');?>">
<?php echo form_error('name','<div class="error">','</div>'); ?>
</div>
<div style="padding:20px 20px">
<label>Upload Image</label>
<input type="file" name="photo">
</div>
<div style="padding:5px 5px">
<label>Upload Video</label>
<input type="file" name="video">
</div>
<div style="padding:10px 10px">
<input type="submit" name="sumit" value="Submit">
</div>
</form>
My Controller:
$this->form_validation->set_rules('name', 'Name', 'trim|required');
if(! $this->form_validation->run()){
$data['useradded'] = "";
}
else{
if(isset($_FILES['photo']['tmp_name'])){
$config['upload_path'] = "./uploads/complaints/";
$config['allowed_types'] = "jpg|png|gif";
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$new_name = "photo_" . rand();
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
if(! $this->upload->do_upload('photo')){
$data['error'] = array('error' => $this->upload->display_errors());
$photo_name = "";
}else{
$photo_name = $new_name;
}
}
if(isset($_FILES['video']['tmp_name'])){
$config['upload_path'] = "./uploads/complaints/videos/";
$config['allowed_types'] = "avi|flv|wmv|mpg|mpeg|mp3|mp4";
//$config['allowed_types'] = "video/x-msvideo|image/jpeg|video/mpeg|video/x-ms-wmv";
$config['max_size'] = '0';
$new_name = "video_" . rand();
$config['file_name'] = $new_name;
$this->load->library('upload', $config);
if(! $this->upload->do_upload('video')){
$data['error'] = array('error' => $this->upload->display_errors());
$video_name = "";
}else{
$video_name = $new_name;
}
}
$this->complaints_model->add_complaint($photo_name,$video_name);
$data['useradded'] = "New complaint added Successfully";
}
$this->load->view('home', $data);

CodeIgniter Upload Class Allowed File Types

I'm using Codeigniter Upload Class but I have a problem with allowed file types. I need to allow only pdf files to upload but any kind of file can be upload. Here is my code;
model (muser.php);
function cv_ekle()
{
$config['upload_path'] = 'uploads/cv';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$this->load->library('upload', $config);
$upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$data = array
(
'userid' => $this->session->userdata('id'),
'kullanici' => $this->session->userdata('isim'),
'kategori' => $this->input->post('kategori'),
'tarih' => time(),
'dosya' => $file_name
);
if($this->db->insert('cv', $data))
{
return true;
}
else
{
return false;
}
}
controller (cv.php);
function cv_ekle()
{
if($this->muser->cv_ekle())
{
$this->session->set_flashdata('ok', 'CV başarıyla gönderildi!');
redirect('cv');
}
else
{
$this->session->set_flashdata('hata', 'Sadece PDF, Excel ya da Word formatında yükleme yapabilirsiniz!');
redirect('cv');
}
}
view (cv.php);
<form method="post" action="<?php echo site_url('cv/cv_ekle'); ?>" class="login" enctype="multipart/form-data">
<div class="controls">
<label for="email">Dosya: <span class="text-error">*</span></label>
<input type="file" id="pass" class="input-block-level" name="userfile" >
</div>
<div class="controls">
<label for="email">Kategori: <span class="text-error">*</span></label>
<select class="input-block-level" name="kategori">
<?php foreach($kategoriler as $kat) { ?>
<option value="<?php echo $kat['isim']; ?>"><?php echo $kat['isim']; ?></option>
<?php } ?>
</select>
</div>
<div class="controls">
<button type="submit" class="btn btn-primary">CV Yükle</button>
</div>
</form>
Thanks in advance!
function cv_ekle()
{
$config['upload_path'] = 'uploads/cv';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_data = $this->upload->data(); //Returns array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$data = array
(
'userid' => $this->session->userdata('id'),
'kullanici' => $this->session->userdata('isim'),
'kategori' => $this->input->post('kategori'),
'tarih' => time(),
'dosya' => $file_name
);
if($this->db->insert('cv', $data))
{
return true;
}
else
{
return false;
}
}

Categories