how to upload different file on the same form - php

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

Related

How to upload files in two different folders using CodeIgniter3?

How to upload images and videos in two different folders using CodeIgniter 3?
I have done only images. Please guide me through videos. I want to upload image and videos in two different folders.
My Controller, Model and View
class Blog extends CI_Contrller{
public function create_blog(){
// Check Login
if(!$this->session->userdata('logged_in')){
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$path = './assets/uploads/blogs/images';
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Model
// Model Begins here
public function create_blog($post_image){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'content' => $this->input->post('content'),
'image' => $post_image
);
return $this->db->insert('events', $data);
}
View
// View begins here
<div class="container"><br>
<h2><?= $title ?></h2>
<?php echo form_open_multipart('blog/create_blog'); ?>
<?php echo validation_errors(); ?>
<?php echo $this->session->flashdata('file_error'); ?>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title"
placeholder="Add Title">
</div>
<div class="form-group">
<label>Body</label>
<textarea class="form-control" id="editor1" name="content"
placeholder="Add Body"></textarea>
</div>
<div class="form-group">
<label>Upload Image</label>
<input type="file" name="userfile" id="userfile" size="20" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
Try this
class Blog extends CI_Contrller{
function create_blog() {
// Check Login
if (!$this->session->userdata('logged_in')) {
redirect('blogs/login');
}
$data['title'] = 'Create Blog';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header');
$this->load->view('blog/create_blog', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
$ext = strtolower($ext);
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg');
if (in_array($ext, $image_ext_arr)) {
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else {
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
$config['upload_path'] = $path;
$config['allowed_types'] = $allowed_types;
$config['max_size'] = 15000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('file_error', $this->upload->display_errors());
$post_image = 'noimage.jpg';
redirect('blog/create_blog');
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->M_blog->create_blog($post_image);
redirect('blog/view');
}
}
}
Added few line :
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION); //to find whether is image or video
$ext = strtolower($ext);// some time extension in upper later so converted into small letters
$image_ext_arr = array('gif', 'jpg', 'png', 'jpeg'); //give here all extension of image that you want to upload
if (in_array($ext, $image_ext_arr)) { //if matches means is image
$path = FCPATH.'assets/uploads/blogs/images';
$allowed_types = 'gif|jpg|png';
} else { //else video
$path = FCPATH.'assets/uploads/blogs/video';
$allowed_types = 'mp4|mp3';
}
Note : Better to give absolute path instead of relative path to upload so
$path = FCPATH.'assets/uploads/blogs/video';

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

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

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

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