CodeIgniter Upload Class Allowed File Types - php

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;
}
}

Related

Php codeigniter is not uploading an image

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());
}

images upload failed in codeigniter

I have a problem with inserting data when the button is submitted, the image upload fails.
I tried several times but failed. how to upload images successfully and stored
so here my code:
My controller:
public function savedosen() {
$this->load->library('upload');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '2048';
$config['max_width'] = '1288';
$config['max_height'] = '768';
$this->upload->initialize($config);
if ($this->upload->do_upload('foto'))
{
$gambar = $this->upload->data();
$nik = $this->input->post('nik');
$nama = $this->input->post('nama_dosen');
$jk = $this->input->post('jk');
$agama = $this->input->post('agama');
$tempat = $this->input->post('tempat');
$title = $this->input->post('title');
$status = $this->input->post('status');
$prodi = $this->input->post('prodi');
$password = $this->input->post('confirm_password');
$data = array(
'nik_dosen' =>$nik,
'nama_dosen' => $nama,
'jenis_kelamin' => $jk,
'agama' => $agama,
'tempat_lahir' =>$tempat,
'status' => $status,
'title_dosen' => $title,
'foto_dosen' => $gambar['file_name'],
'pass_dosen' => md5($password),
'id_prodi' => $prodi,
);
$this->dosen_model->insertdosen($data);
redirect('dosen');
}
else
{
echo "Gagal";
}}
My view:
<div class="form-group ">
<label for="firstname" class="control-label col-lg-2">Upload Gambar
<span class="required"> * </span></label>
<div class="col-lg-6">
<input class=" form-control" id="foto" name="foto" type="file" />
</div>
</div>
Help me to resolve it.
change
$this->load->library('foto');
to
$this->load->library('upload', $config);

Image not uploading Codeigniter

My image upload gives following error
Array ( [error] =>
You did not select a file to upload.
)
Here is the html
<div class="form-group">
<label class="col-sm-3 control-label">Product Image</label>
<div class="col-sm-9">
<input type="file" name="item_image" />
</div>
</div>
Here is the controller
$config['upload_path'] = './images/item_images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$error = '';
$udata = '';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('item_image')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$udata = array('upload_data' => $this->upload->data());
print_r($udata);
$item_data['image_url'] = "images/item_images/" . $udata['upload_data']['file_name'];
}
I can't fix my error

how to upload different file on the same form

i try to upload different file on the same form. I want to add my file in my directory and name of file into my database. I can upload file in directory but into database it doesn't work.
this my controller
function tambahdata()
{
$this->load->view('vtambah_mastersub');
if ($this->input->post('submit')) {
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '40000';
$config['max_width'] = '1288';
$config['max_height'] = '1288';
$config['file_name'] = "file_" . time();
$this->load->library('upload');
$this->upload->initialize($config);
// if there was an error, return and display it
if (!$this->upload->do_upload('picture')) {
echo "errornya : ";
echo $this->upload->display_errors();
} else {
echo "sukses";
$gbr = $this->upload->data();
$data = array(
'nm_gbr' => $gbr['file_name'],
'tipe_gbr'=>$gbr['file_type'],
) ;
}
}
if (!empty($_FILES['pdf']['name'])) {
$config['upload_path'] = './assets/uploads';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '40000';
$config['max_width'] = '1288';
$config['max_height'] = '1288';
$config['file_name'] = "file_" . time();
$this->load->library('upload');
$this->upload->initialize($config);
// if there was an error, return and display it
if (!$this->upload->do_upload('pdf')) {
echo "errornya : ";
echo $this->upload->display_errors();
} else {
echo "sukses";
$pdf = $this->upload->data();
$dataa = array(
'nm_pdf' => $pdf['file_name'],
'tipe_pdf' => $pdf['file_type'],
);
}
}
$this->m_mastersub->tambah($data,$dataa);
redirect('mastersub');
}
}
this my model
function tambah($gambar,$pdf){
$kd_mastersub = $this->input->post('kd_mastersub');
$nama = $this->input->post('nama');
$picture = $gambar['nm_gbr'];
$deskripsi = $this->input->post('deskripsi');
$pdf = $pdf('nm_pdf');
$video = $gambar('nm_video');
$drive = $gambar('nm_drive');
$data = array(
'kd_mastersub' =>$kd_mastersub,
'name_cate' =>$nama,
'picture' => $picture,
'description' =>$deskripsi,
'pdf' => $pdf,
'video' => $video,
'drive' => $drive
);
$this->db->insert('tbl_mastersub',$data);
}
this my form
<div class="row form-group">
<div class="col-md-6 text-left">
<label>Picture </label>
</div>
<div class="col-md-10 text-left">
<input type="file" name="picture" />
</div>
</div>
<div class="row form-group">
<div class="col-md-6 text-left">
<label>PDF </label>
</div>
<div class="col-md-10 text-left">
<input type="file" name="pdf" />
</div>
</div>
this my error
any one can help me to solve this problem please, thanks
it looks like there are some typos in your code
try this instead
function tambah($gambar,$pdf){
$kd_mastersub = $this->input->post('kd_mastersub');
$nama = $this->input->post('nama');
$picture = $gambar['nm_gbr'];
$deskripsi = $this->input->post('deskripsi');
$pdf = $pdf['nm_pdf'];
$video = $gambar['nm_video'];
$drive = $gambar['nm_drive'];
$data = array(
'kd_mastersub' =>$kd_mastersub,
'name_cate' =>$nama,
'picture' => $picture,
'description' =>$deskripsi,
'pdf' => $pdf,
'video' => $video,
'drive' => $drive
);
$this->db->insert('tbl_mastersub',$data);
}

image upload in codeigniter giving error

I am upload an image in one my form but its always giving me an error like
"A PHP Error was encountered Severity: Notice Message:
Undefined index: vimage Filename: controllers/vouchers.php Line
Number: 42"
My View File code :
<?php echo form_open_multipart(site_url("promotions/add_item/"), array("class" => "form-horizontal","id"=>"addItem")) ?>
<div class="form-group">
<label class="col-sm-3 control-label">Show on</label>
<div class="col-sm-3">
Artist Directory : <input type="checkbox" name="artist_dir" value='1'>
</div>
<div class="col-sm-3">
Highlight : <input type="checkbox" name="highlight" value='1'>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Title</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="title" id="title" placeholder="Title">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Image</label>
<div class="col-sm-9">
<input type="file" name="pro_image">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Description</label>
<div class="col-sm-9">
<textarea class="form-control" rows="3" name="description" placeholder="Description"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button>
</div>
</div>
<?php echo form_close() ?>
My Controller Code :
<pre>
public function add_item(){
$this->load->library('upload');
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
print_r($_FILES['pro_image']);
if ($_FILES['pro_image']['size'] > 0) {
$this->upload->initialize(array(
"upload_path" => './uploads/',
"overwrite" => FALSE,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "gif|jpg|png|jpeg",
));
if (!$this->upload->do_upload('vimage')) {
$this->upload->display_errors();
}
$data = $this->upload->data();
echo 'img : '.$img = $data['file_name'];
}
exit;
if($this->form_validation->run()==FALSE){
echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'. validation_errors().'</small></div>';
}
else {
$title = $this->input->post('title');
$description = $this->input->post('description');
$artist_dir = $this->input->post('artist_dir');
$highlight = $this->input->post('highlight');
$this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
}
}
</pre>
please let me know what i am doing wrong here
Try this
if ($_FILES['vimage']['size'] > 0) {
$this->load->library('upload');
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$fileName = $_FILES['vimage']['name'];
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;
}
For more read this Link
Try this
if (!empty($_FILES['vimage']))
{
$config['upload_path'] = './uploads/';
//$config['max_size'] = '102400';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('vimage'))
{
$this->session->set_flashdata('error', $this->upload->display_errors());
//redirect('controller/method');
}
else
{
$this->session->set_flashdata('success', 'Image Has been Uploaded');
//redirect('controller/method');
$img = $data['file_name'];
}
}
Please check first,
print_r(_FILES);
If you get files array then you can use below code for uploading image.
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';
// You can give Image formats if you want to upload any video file.
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' = $this->upload->display_errors());
// uploading failed. $error will holds the errors.
}
else
{
$data = array('upload_data' => $this->upload->data());
// uploading successfull, now do your further actions
}
}
I have added your code in my system it's working for fine for me.
public function add(){
$this->load->library('upload');
$this->load->helper('form');
$this->load->helper('url');
print_r($_FILES);die;
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('description','Description','required');
if ($_FILES['pro_image']['size'] > 0) {
$this->upload->initialize(array(
"upload_path" => './uploads/',
"overwrite" => FALSE,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "gif|jpg|png|jpeg",
));
if (!$this->upload->do_upload('vimage')) {
$this->upload->display_errors();
}
$data = $this->upload->data();
echo 'img : '.$img = $data['file_name'];
}
exit;
if($this->form_validation->run()==FALSE){
echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'. validation_errors().'</small></div>';
}
else {
$title = $this->input->post('title');
$description = $this->input->post('description');
$artist_dir = $this->input->post('artist_dir');
$highlight = $this->input->post('highlight');
$this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
}
}

Categories