How to upload php or html file in codeigniter using upload library? I have controller which working ok when uploading jpg, gif, png files but it won't to upload files with php or html or sql extension.
Here is my controller
public function ipload()
{
$this->load->helper('url');
$this->load->model('m_company');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'php|html|txt';
$config['max_size'] = 2048;
$config['max_width'] = 0;
$config['max_height'] = 0;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
$data = array('error' => $this->upload->display_errors());
}
else
{
$this->upload->do_upload('userfile');
$data = array('upload_data' => $this->upload->data());
$filen = $this->upload->data('file_name');
$this->m_company->set_doc($filen);
}
echo json_encode($data);
}
When allowed files are jpg gif png everything works fine
first you setting controler, you can use access to upload file all class using $config['allowed_types'] ='*';
class c_cutipegawai extends CI_Controller{
function __construct(){
parent::__construct();
}
function do_upload(){
$config['upload_path'] ='./upload/SuratCuti/';
$config['allowed_types'] ='*';
$config['max_size'] =1024*8;
$config['max_widht'] =1024*2;
$config['max_height'] =768;
$this->load->library('upload', $config);
if(! $this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array('upload_data' => $this->upload->data());
}
}
}
After setting in controler, now we craete view to insert button upload file
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo form_open_multipart('C_CutiPegawai/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Related
I am trying upload file from CodeIgniter. File not giving any error and even not storing file in uploads folder whic is in root directory or codeigniter. Can somebody help ? Please note as i already gave 755 permission to my uploads folder. below is my code
view file
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('UploadController');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Below is my controller
<?php
class UploadController extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('uploadview', array('error' => ' ' ));
}
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';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload("add_1"))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('uploadview', $data);
}
}
}
?>
please help me in this issue.
there are some mistakes in your code:
Please changes on following lines:
1.change
form_open_multipart('UploadController')
to
form_open_multipart('UploadController/do_upload')
2.change
$this->upload->do_upload("add_1")
to
$this->upload->do_upload("userfile").
3.change
$this->load->view('upload_form', $error);
to
$this->load->view('uploadview', $error);
change
form_open_multipart('UploadController')
to
form_open_multipart('UploadController/do_upload')
change your form action path
form_open_multipart('UploadController/do_upload')
or you can use it like this in one method:
function index()
{
$config['upload_path'] = 'upload_path';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 5120;
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
$this->form_validation->set_rules('image', 'lang:image', 'file_size_max[5M]|file_allowed_type[image]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('uploadview', array('error' => ' ' ))
}
else
{
$user = array();
$uploaded = $this->upload->do_upload('add_1');
//delete the original file if another is uploaded
if($uploaded)
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('uploadview', $data);
} else {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
}
And make sure your upload path is right.
I am trying to simply upload a image to a specified folder but cant figure out why is the controller function not working.Here is the controller code
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '400';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if($this->upload->do_upload()){
echo "image uploaded";
}
echo $this->input->post('username');
}
And this is the form to input image file in view folder
<form enctype="multipart/form-data" method="post" action="<?php echo site_url('welcome/do_upload');?>">
username:<input type="text" name="username">
<p>upload file</p>
<input type="file" name="image">
<input type="submit" name="submit" value="submit">
</form>
You just need to pass file input name 'image' in do_upload function, by default its 'userfile'
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '400';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if($this->upload->do_upload('image')){ //passed 'image' file control input name in form
echo "image uploaded";
}
echo $this->input->post('username');
}
I see CI documentation to create an image uploader.
here is my Controller :
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$this->load->helper(array('form', 'url'));
$this->load->helper('url');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_error();
return FALSE;
}
else
{
$data = $this->upload->data();
return TRUE;
}
}
}
and here is my View
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<form action="upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
But when i upload an image it doesn't upload it and $this->upload->display_error() show nothing(my uploads folder which is in my ci root folder also has 777 permision and i'm using wamp)
Two pointers for you:
<input type="file" name="userfile" size="20" />
Check if the file you're using is not exceeding and the file is being uploaded.
echo $this->upload->display_errors();
See the display_errors with an s.
Suggestions:
In you form's action attribute use a an absolute URL, like:
action="<?php echo base_url() ?>upload/do_upload"
Inside your function do_upload(), do a print_r($_FILEs), to confirm that the file is uploaded correctly with no errors.
I change my function named function do_upload() to function somethingelse() and my problem solved (because i'm calling a built in function)
I have made a code that let me upload multiple files but in separate
I was trying to upload a multiple files in one input where i set my input tag into
<input type="file" multiple="" name="file1">
I selected 3 images but only 1 image was uploaded..
here is my VIEW before changing my input:
<?php echo form_open_multipart('test'); ?>
<p>
<?php echo form_label('File 1: ', 'file1') ?>
<input type='file' name='file1' id='file1'>
</p>
<p>
<?php //echo form_label('File 2: ', 'file2') ?>
<input type='file' name='file2' id='file2'>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
and here is my CONTROLLER
function index()
{
if (isset($_POST['submit']))
{
$this->load->library('upload');
$config['upload_path'] = './upload_documents/';
$config['allowed_types'] = 'jpg|png|gif|jpeg|JPG|PNG|GIF|JPEG';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
foreach($_FILES as $field => $file)
{
// No problems with the file
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = $this->upload->data();
//alert("nice");
}
else
{
$errors = $this->upload->display_errors();
die();
}
}
else{
echo "alksjdfl";
die();
}
}
}
$this->load->view("test");
}
}
Use this multi file upload library
https://github.com/stvnthomas/CodeIgniter-Multi-Upload/blob/master/MY_Upload.php
// Prepraing upload config & upload files
$config = array();
$config['upload_path'] = 'temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '256000';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_multi_upload("my_file");
$files_upload = $this->upload->get_multi_upload_data();
$err_msg = $this->upload->display_errors();
if (!empty($files_upload) && is_array($files_upload)) {
foreach ($files_upload as $file_data) {
// Your files here :)
}
}
Special thanks to #stvnthomas for the library :)
You should use this library for multi upload in CI
https://github.com/stvnthomas/CodeIgniter-Multi-Upload
Installation
Simply copy the MY_Upload.php file to your applications library directory.
Use: function test_up in controller
public function test_up(){
if($this->input->post('submit')){
$path = './public/test_upload/';
$this->load->library('upload');
$this->upload->initialize(array(
"upload_path"=>$path,
"allowed_types"=>"*"
));
if($this->upload->do_multi_upload("myfile")){
echo '<pre>';
print_r($this->upload->get_multi_upload_data());
echo '</pre>';
}
}else{
$this->load->view('test/upload_view');
}
}
upload_view.php in applications/view/test folder
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile[]" id="myfile" multiple>
<input type="submit" name="submit" id="submit" value="submit"/>
I got a problem with multiple upload file using codeigniter. I've tried many method before but still no success.
Here is my code.
View
<?php echo form_open_multipart('staff/upload_files', array('id' => 'form_checkbox')); ?>
Inside table:
<tr>
<th width="20%">Select File :</th>
<td>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span><br>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span><br>
<input type="file" name="userfile[]" value="" /> <span class="green">[Max File Upload: 2MB]</span>
</td>
</tr>
Controller
function upload_files() {
$data['studentid'] = $studentid;
if ($this->input->post('submit')) {
$config['upload_path'] = './upload/announcement/collegecode/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('msg', 'error: ' . $this->upload->display_errors());
} else {
$this->session->set_flashdata('msg', 'success:Successfully Upload Student Photo');
}
}
$this->template->load('template', 'staff/announcement', $data);
}
I've also tried this method, but no file was uploaded.
$config['upload_path'] = './upload/announcement/collegecode/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value)
{
if (!empty($key['userfile']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($key))
{
$errors = $this->upload->display_errors();
flashMsg($errors);
}
else
{
// Code After Files Upload Success GOES HERE
}
}
}