My Controller :
$foto = $_FILES['foto']['name'];
$doc = $_FILES['doc']['name'];
$ambextFoto = explode(".",$foto);
$ambextDOc = explode(".",$doc);
$ekstensiFoto = end($ambextFoto);
$ekstensiDoc = end($ambextDoc);
$nama_baru_foto = 'foto-'.date('YmdHis');
$nama_baru_doc = 'doc-'.date('YmdHis');
$nama_file_foto = $nama_baru_foto.".".$ekstensiFoto;
$nama_file_doc = $nama_baru_doc.".".$ekstensiDoc;
$config['upload_path'] = './assets/userfiles/usulan/';
$config['allowed_types'] = 'pdf';
$config['file_name'] = $nama_file_foto;
$config['file_name'] = $nama_file_doc;
$this->upload->initialize($config);
if($this->upload->do_upload('foto') AND $this->upload->do_upload('doc')){
$data = array(
'member_id' => $this->session->userdata('member_id'),
'foto' => $foto,
'doc' => $doc
);
}else{
$this->session->set_flashdata('error','Upload File Failed');
redirect(site_url('data/usulan/add'));
}
My View :
<form action="<?php echo site_url('data/usulan/insert') ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" class="form-control" name="foto" accept="application/pdf">
</div>
<div class="form-group">
<input type="file" class="form-control" name="doc" accept="application/pdf">
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary ml-auto">Submit</button>
</div>
</form>
The files was uploaded but the name file upload only in variable $name_file_doc. All file was uploaded but only the name is of the name $name_file_doc.
Please help me
Use this:
if(isset($_FILES['foto']['name'])){
//write only foto file upload code here.
}
if(isset($_FILES['doc']['name'])){
//write only doc file upload code here.
}
Related
I've use do upload for my image on first time, and thats worked fine.
Then i want to upgrade for upload a pdf,doc, and xls.
Image, and xls worked, there is the file in upload_folder , and the file worked to download.
The problem when i trying to upload a pdf and docs, the file isnt uploaded to upload_folder
this is my code :
mimes.php
'pdf' => array('application/pdf', 'application/force-download', 'application/x-download', 'binary/octet-stream'),
core.php for download file
function download($src)
{
$this->load->helper('download');
force_download('uploads/'.$src, NULL);
}
insert.php for upload file
function insert(){
$id_car_header = $this->input->post('input_id_car_header');
$keterangan = $this->input->post('input_keterangan');
$arsip = $_FILES["input_arsip"]['name'];
if ($arsip ='')
{}
else
{
$config ['allowed_types'] = 'jpg|jpeg|png|docx|pdf|xls';
$arsip_generate =$_FILES["input_arsip"]['name'];
$config ['upload_path'] = './uploads';
$config['file_name'] = $arsip_generate;
$this->load->library('upload', $config);
$this->load->library('upload', $config);
//$this->upload->initialize($config);
if (!$this->upload->do_upload('input_arsip'))
{
//$this->upload->display_errors();
echo "Gambar Gagal DiUpload..!!";
} else{
$arsip = $this->upload->data('file_name');
}
}
$data = array
(
'id_car_header' => $id_car_header,
'ket_arsip' => $keterangan,
'datas' => $arsip
);
$check = $this->m_db->insertDataAll("tbl_car_arsip", $data);
$this->checkInsert($check);
}
my layout as form
<form action="<?php echo base_url().'/insert/insertArsip'; ?>" method="post" enctype="multipart/form-data">
<div class="col-md-12 col-xs-12">
<div class="form-group">
<input type="hidden" name="input_id_car_header" value="<?= $carHeader['id_car_header']?>">
<label class="control-label col-md-3 col-sm-3 col-xs-12">File/Foto </label>
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="file" name="input_arsip" class="form-control">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Keterangan </label>
<div class="col-md-9 col-sm-9 col-xs-12">
<textarea name="input_keterangan" required="required" class="form-control" rows="3" placeholder="Masukan Keterangan"></textarea>
</div>
</div>
<div class="ln_solid"></div>
</div>
<div class="form-group">
<div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-3">
<button type="submit" class="btn btn-success pull-right">
<i class="fa fa-save"></i> Save
</button>
</div>
</div>
</form>
I have trying to change my mime.php, and initialize, and change the structure code but all of that not working
Can somebody tell me what i need to do to make my code work for upload pdf and docs?
thank you very much
I dont know why this works after shut my laptop off and change the code little bit, all file with big size can be uploaded and downloaded
ofc i change the code from some user at Stackoverflow that having problem like me, thanks for that
i thinks this one that make uploaded pdf worked, i change this line
$config ['allowed_types'] = '*';
this is the update for upload file, i only change this function
$id_car_header = $this->input->post('input_id_car_header');
$keterangan = $this->input->post('input_keterangan');
$arsip = $_FILES["input_arsip"]['name'];
$config ['allowed_types'] = '*';
$arsip_generate =$_FILES["input_arsip"]['name'];
$config ['upload_path'] = './uploads';
$config['file_name'] = $arsip_generate;
$config['max_size'] = 2000000;
$config['max_width'] = 1500;
$config['max_height'] = 1500;
$this->load->library('upload', $config);
$this->upload->initialize($config, true);
//$this->upload->initialize($config);
if (!$this->upload->do_upload('input_arsip'))
{
$error = array('error' => $this->upload->display_errors());
$this->upload->display_errors();
//$this->upload->display_errors()
echo "Gambar Gagal DiUpload..!!";
var_dump($config);
} else{
$arsip = $this->upload->data('file_name');
$data = array
(
'id_car_header' => $id_car_header,
'ket_arsip' => $keterangan,
'datas' => $arsip
);
$check = $this->m_db->insertDataAll("tbl_car_arsip", $data);
$this->checkInsert($check);
}
I am trying to do a file uploader in codeigniter that would save the name of the file in a phpmyadmin database. when I add a "foto" the actual name of the file isn't saved.
Controller ctr_fotos.php:
public function add2()
{
$data['id_evento'] = $this->input->post('id_evento');
//file upload code
//set file upload settings
$config['upload_path'] = APPPATH. '../images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('foto')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('nova_foto', $error);
}else{
//file is uploaded successfully
//now get the file uploaded data
$upload_data = $this->upload->data();
//get the uploaded file name
$data['foto'] = $upload_data['file_name'];
//store pic data to the db
}
$this->load->model('fotos_model');
$this->fotos_model->RegistarFoto($data);
redirect('Ctr_fotos/all');
}
Model fotos_model.php:
public function RegistarFoto($data)
{
$insert_data['id_evento'] = $data['id_evento'];
$insert_data['foto'] = $data['foto'];
$this->db->insert('fotos', $insert_data);
}
view nova_foto.php:
<body>
<div class="jumbotron">
<h1>Criar Foto</h1>
</div>
<div class="container">
<?php
echo form_open("Ctr_fotos/add2");
?>
<div class="form-group">
<label for="nome" class="control-label">Id do Evento</label>
<input type="text" class="form-control" name="id_evento" value="<?php echo set_value('id_evento');?>">
</div>
<div class="form-group">
<label for="texto" class="control-label">Texto</label>
<input type="file" class="form-control" name="foto" id="foto">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="bt_submeter" value="Adicionar">
</div>
Change your code to
}else{
//file is uploaded successfully
//now get the file uploaded data
$upload_data = $this->upload->data();
//get the uploaded file name
$data['foto'] = $upload_data['file_name'];
//store pic data to the db
$this->load->model('fotos_model');
$this->fotos_model->RegistarFoto($data);
redirect('Ctr_fotos/all');
}
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 working in form to upload image in project folder and store the other data into Database. when I try to submit this form data was submitted and image was not uploaded and one more the image was uploaded and not all of data was stored in database this is my code
model:
function update_news($data) {
extract($data);
$this->db->where('news_id', $news_id);
$this->db->update($table_name, array('title_en' => $title_en, 'title_ar' => $title_ar,'news_date' => $news_date,'image' => $image,'is_visible' => $is_visible,'main_news' => $main_news));
return true;
}
controller
public function update() {
$filename = $this->input->post('image');
$date = DateTime::createFromFormat("Y-m-d", $this->input->post('news_date'));
$data = array(
'table_name' => 'news', // pass the real table name
'news_id' => $this->input->post('news_id'),
'title_en' => $this->input->post('title_en'),
'title_ar' => $this->input->post('title_ar'),
'news_date' => $this->input->post('news_date'),
'image' => $date->format("Y") . '/' . $this->input->post('image'),
'is_visible' => $this->input->post('is_visible'),
'main_news' => $this->input->post('main_news')
);
$this->m_news_crud->update_news($data);
$folderName = 'assets/images/press-news/2015';
$config['upload_path'] = "$folderName";
if (!is_dir($folderName)) {
mkdir($folderName, 0777, TRUE);
}
$config['file_name'] = $this->input->post('image');
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile','image')) {
$error = array('error' => $this->upload->display_errors());
$data['error'] = $error;
$data['title_en'] = $this->input->post('title_en');
$data['title_ar'] = $this->input->post('title_ar');
$data['news_date'] = $this->input->post('news_date');
$data['image'] = base_url('assets/images/press-news/' . $date->format("Y")) . '/' . $this->input->post('image');
$data['is_visible'] = $this->input->post('is_visible');
$data['main_news'] = $this->input->post('main_news');
$data['news_id'] = $this->input->post('news_id');
$this->load->view('admin/news/d_admin_header');
$this->load->view('admin/news/vcrudedit_news', $data);
$this->load->view('admin/news/d_admin_footer');
} else {
$data = array('upload_data' => $this->upload->data());
redirect('c_news_crud/get_news_data', $data);
echo $filename;
}
}
view:
<div class="container">
<div class="col-md-9">
<?php echo form_open('C_news_crud/update', 'role="form" style="margin-top: 50px;"'); ?>
<div class="form-group">
<label for="title_en">Title Name English</label>
<input type="text" class="form-control" id="title_en" name="title_en" value="<?php echo $title_en ?>">
</div>
<div class="form-group">
<label for="title_ar">Title Name Arabic</label>
<input type="text" class="form-control" id="title_ar" name="title_ar" value="<?php echo $title_ar ?>">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<label for="is_visible">is visible</label>
<?php
if ($is_visible == 1) {
$check_visible = 'checked="checked"';
} else {
$check_visible = ' ';
}
?>
<input role="checkbox" type="checkbox" id="is_visible" class="cbox" <?php echo $check_visible; ?> name="is_visible" value="<?php echo $is_visible; ?>">
</div>
<div class="col-md-4">
<label for="main_news">Main Arabic</label>
<?php
if ($main_news == 1) {
$check_main = 'checked="checked"';
} else {
$check_main = ' ';
}
?>
<input role="checkbox" type="checkbox" id="main_news" class="cbox" <?php echo $check_main; ?> name="main_news" value="<?php echo $main_news; ?>">
</div>
</div>
<div class="form-group">
<label for="news_date">News date</label>
<input type="date" class="form-control" id="news_date" name="news_date" value="<?php echo $news_date ?>">
</div>
<div class="form-group">
<label for="image">image</label>
<input type="file" class="form-control" id="image" name="image" value="<?php echo $image ?>">
</div>
<input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
<input type="submit" name="save" class="btn btn-primary" value="Update">
<button type="button" onclick="location.href = '<?php echo site_url('C_news_crud/get_news_data') ?>'" class="btn btn-success">Back</button>
</form>
<?php echo form_close(); ?>
</div>
Make sure the path to the uploading folder is correct and it has write permission.
Make sure you are referring to the correct file input name:
$config['file_name'] = $_FILES['image']['name'];
set your uploading directory as ./assets/images/press-news/2015/
You need to display the error you get so that you know what is causing the error!
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
Codigniter's Upload Class
I tried many ways to upload Videos through my website. I don't know why, I can save images, text files etc. but it is not possible to save videos into the folder and database.
Here is my view.
<form action="<?php echo base_url();?>galleries/add_vdo" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter Title" name="vdo_title" id="vdo_title">
<textarea class="form-control" placeholder="About Video" name="vdo_desc" id="vdo_desc"></textarea>
<input class="" name="ur_vdo" id="ur_vdo" type="file">
<input class="btn btn-info" type="submit" name="submit" value="Add Now"/>
<!--<button class="btn btn-info btn-lg" id="submit" type="button">Invite</button>-->
</div>
</form>
controller:
function add_vdo()
{
$data['ur_vdo'] = $this->gallery_model->add_new_vdos();
redirect('galleries/vdo_gal','refresh');
}
Model :
function add_new_vdos()
{
$this->load->helper('date');
date_default_timezone_set('Asia/Calcutta');
$config['upload_path'] = './uploads/myvideos/';
$config['allowed_types'] = 'mp4|avi|flv|wmv|';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name ="ur_vdo";
$rgpic="0";
if ( !$this->upload->do_upload($field_name))
{
$error = $this->upload->display_errors();
$data['error']=$error;
}
else
{
$data = $this->upload->data();
$regpicname=$data['file_name'];
$rgpic="1";
}
if($rgpic!=0)
{
$dts = array(
'familypost_caption'=>$this->input->post('vdo_title'),
'familypost_desc'=>$this->input->post('vdo_title'),
'user_id' =>$uid,
'familypost_vdo'=>$regpicname,
'familypost_status'=>'A',
'familypost_date'=>now()
);
}
else
{
$dts = array(
'familypost_caption'=>$this->input->post('vdo_title'),
'familypost_desc'=>$this->input->post('vdo_title'),
'user_id' =>$uid,
'familypost_status'=>'A',
'familypost_date'=>now()
);
}
$vdqr = $this->db->insert('roottildb_familyposts',$dts);
}
I don't know why, video upload fails without showing any error. How can I solve this issue?