I'm trying to upload a file. I select a file and then I submit it but result of $this->upload->do_upload() is always false.
Here is my form;
<?php echo form_open_multipart(base_url('files/fileUpload')); ?>
<input type="file" name="userfile" class="btn btn-default" size="20"/>
<input type="submit" value="upload" />
</form>
and here is my controller;
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function fileUpload(){
$this->load->library('upload');
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Do I miss something ? What do I need to do ?
Thank you..
There is problem with code in controller fileUpload function
Try with below code
public function fileUpload(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload',$config);
$data = array('upload_data' => $this->upload->data());
var_dump($this->upload->do_upload());die;
}
Here you need to define file upload path and file allow types.
Hope this will help you.
Related
I have a problem with how to upload 2 file input in 1 form CI 3 with different file name. My view code: There is have 2 file input, 1. file_sertifikat and 2. file_rekomendasi
<div class="form-group">
<label for="file_sertifikat">File Sertifikat</label>
<input type="file" name="file_sertifikat" class="form-control" id="file_sertifikat">
<small class="text-danger"><?= form_error('file_sertifikat') ?></small>
</div>
<div class="form-group">
<label for="file_rekomendasi">File Rekomendasi</label>
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
<small class="text-danger"><?= form_error('file_rekomendasi') ?></small>
</div>
My Controller action to save filename to database:
$file_sertifikat = $this->upload_file_sertifikat($this->input->post('file_sertifikat'));
$file_rekomendasi = $this->upload_file_rekomendasi($this->input->post('file_rekomendasi'));
$data = array(
'file_sertifikat' => $file_sertifikat,
'file_rekomendasi' => $file_rekomendasi,
);
$this->perizinan_model->insert_data($data,'perizinan');
redirect('admin/perizinan');
}
and my upload file function:
public function upload_file_sertifikat(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_sertifikat_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_sertifikat')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
public function upload_file_rekomendasi(){
$config['upload_path'] = './assets/uploads';
$config['file_name'] = 'file_rekomendasi_'.rand(1, 99999);
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf';
$config['remove_space'] = TRUE;
$config['max_size'] = 10048;
$this->load->library('upload', $config);
if($this->upload->do_upload('file_rekomendasi')){
return $this->upload->data("file_name");
}else{
return "defaultimg.png";
}
}
I want to upload that 2 different filename one by one, but my code overwrite the file. Thank before.
In html change
<input type="file" name="file_rekomendasi" class="form-control" id="file_rekomendasi">
to
<input type="file" name="file_rekomendasi[]" class="form-control" id="file_rekomendasi" multiple >
In the controller, you will get $file_rekomendasi as an array. Handel that as like array. Run a loop.
I've been trying to get this to work since this morning, but have no idea why it doesn't. The uploaded image gets stored in the database, but when I check my upload folder --where I want to store the images -- there's nothing there.
Can anyone help me with this problem?
Home.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Contact_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
public function Latest_news()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if (($this->form_validation->run() == FALSE )&&(!$this->upload->do_upload('filename')))
{
$this->load->view('Latest_news');
}
else
{
//insert
$data = array();
$data['first_content']=$this->input->post('first_content');
$data['second_content']=$this->input->post('second_content');
$data['filename']=$this->input->post('filename');
//Transfering data to Model
$this->Contact_model->latest_news($data);
//Redirecting to success page
redirect(site_url('Home/Latest_news'));
}
}
?>
Contact_model.php
<?php
class Contact_model extends CI_Model
{
function latest_news($data)
{
//saving records
$this->db->insert('latest_news', $data);
}
}
?>
Latest_news.php
<?php echo form_open(); ?>
<div style="padding-top:10%; padding-left:30%">
<div>
<textarea name="first_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("first_content");?></span>
</div><br>
<div>
<textarea name="second_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("second_content");?></span>
</div><br>
<div>
<input type="file" name="filename">
<span><?php echo form_error("filename");?></span>
</div><br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
Use function form_open_multipart() in Latest_news.php instead form_open()
I am try upload the file with Postman,but get the wrong message:You did not select a file to upload. I do not know what is the reason, but also trying to find ways.
Look at the gif of Postman:
Look at the php code:
public function upload_post()
{
$this->load->helper(array('form', 'url'));
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload',$config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->set_response($data, REST_Controller::HTTP_CREATED);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->response($error, REST_Controller::HTTP_BAD_REQUEST);
}
}
Look at the html:
<?php echo $error;?>
<?php echo form_open_multipart('api/example/upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Upload file with the browser is OK!
Two code is OK!Browser upload no problem!But can not upload with Postman!
Has been solved, the original is not added $this->upload->do_upload('file') and key file postman.
could you put your form code ? i think you forgot to add enctype='multipart/form-data'
I have some problem in image uploading with php codeigniter.
this is my controller
public function FunctionName()
{
$profile_pic = $this->input->post('profile_pic');
echo $profile_pic;
}
It is a simple function and I just want to see the name of the file (image) which I select.
this is my view
<form class="" method="post" action="<?=base_url();?>Controller/FunctionName" enctype="multipart/form-data">
<input type="file" name="profile_pic">
<input type="submit" name="submit">
</form>
When I run this script. It shows me a blank page. I cannot get/display the name of the file which I selected. Note that I have auto-loaded the helper. E.g.
$autoload['helper'] = array('url', 'file', 'date', 'form');
$file=$_FILES['file_name']['name'];
$config = array('file_name'=>$file,
'upload_path' => "./folder/",
'allowed_types' => "jpg|jpeg|png",
'remove_spaces' => FALSE,
'overwrite' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('file_name');
when I click button then it will return error "You did not select a file to upload." and My image is not uploaded!
now please help me what can I do?
my view is
<form action="insertform" enctype="multipart" name="form" method="post" id="contact-form" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Image Upload:</label>
<div class="controls">
<input type="file" class="input-large" name="image" id="image">
</div>
<form>
my controller is:
public function upload()
{
$this->load->helper(array('form', 'url'));
$config['upload_path'] = FCPATH."upload";
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('xyz', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('xyz', $data);
}
}
I think the file input name should be "userfile", not "image".
CI file upload file control default name is userfile.
If you want to change the it's name to customized one, then you have to pass the new name to upload function like :
if ( ! $this->upload->do_upload("image")) // image is your new name for file control
Firstly,
either change the do_upload function call to:
if ( ! $this->upload->do_upload('image'))
OR
change the input name to this:
<input type="file" class="input-large" name="userfile" id="image">
Secondly,
change your form enctype to:
enctype="multipart/form-data"