2

I'm trying to add a file upload function in my website using codeigniter's upload library.

here's my view file (display.php):

<html>
<body>
    <form method="post" action="" enctype="multipart/form-data">
        <input type="file" name="filename" />
        <input type="submit" name="submit" id="submit" value="submit"/>
    </form>
</body>
</html>

and here's the controller:

public function testupload()
{
  if ( ! empty($_FILES))
  {
    echo 'start upload';
    $config['upload_path'] = './assets/img/tempfile/';
    $this->load->library('upload');
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload('filename'))
    {
      echo 'error!';
    }
    else
    {
      echo 'success!';
    }
    echo 'end upload';
  }
  $this->load->view('display', $this->data);
}

but the code seems to stop after $this->upload->initialize($config); the file was not uploaded, and there was no message at all. only the 'start upload' message appeared; the echo 'success' , echo 'error' , and echo 'end upload' do not appear.

why is that? can anyone help me??

8
  • 1
    use $this->upload->display_errors() to debug the errors Commented Dec 27, 2016 at 7:40
  • 1
    enable the debug mode. ini_set('display_errors', '1'); error_reporting(E_ALL); and print this one as well $this->upload->display_errors();
    – Naga
    Commented Dec 27, 2016 at 7:41
  • nope. nothing is working. not even a simple echo 'blabla'; worked if put after $this->upload->do_upload('filename'); I tried @Naga 's advice but still nothing.
    – dapidmini
    Commented Dec 27, 2016 at 7:43
  • Your action is empty try action="<?php echo base_url('controller/function');?>" don't for get to autoload url helper
    – user4419336
    Commented Dec 27, 2016 at 8:13
  • I thought if the action attribute is empty string, it will point to the same function? anyway, I tried putting the uploading part to another function but still the same. only the start upload message appeared, but nothing after that.
    – dapidmini
    Commented Dec 27, 2016 at 8:23

10 Answers 10

1

Late from party, but maybe this can help somebody with same problem. Please try this:

Views:

<?php echo form_open_multipart('test/upload');?>
    <input type="file" name="photo">
<?php echo form_close();?>

Controller:

class Test extends CI_Controller {
    function upload() {
        $config = array(
            'upload_path'  => './assets/upload/',
            'allowed_types'=> 'gif|jpg|png',
            'encrypt_name' => TRUE // Optional, you can add more options as need
        );
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('photo')) {
            echo '<pre>';
            print_r($this->upload->display_errors());
            exit();
        } else {
            echo '<pre>';
            print_r($this->upload->data());
            exit();
        }
    }
}

Inside views i recomended use this function form_open_multipart('ctrl/method') but if you prefer using HTML5 forms, just be sure correct the attributes in form like this.

<form action="<?=site_url('ctrl/method')?>" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <input type="file" name="photo">
</form>

More preferences in $config['..'] can you find in documentation CodeIgniter https://codeigniter.com/user_guide/libraries/file_uploading.html

0

Try like this....

public function testupload()
{
  if ( ! empty($_FILES))
  {
    echo 'start upload';
    $config['upload_path'] = './assets/img/tempfile/';
    $this->load->library('upload',$config);
    if ( ! $this->upload->do_upload('filename'))
    {
       $error = array('error' => $this->upload->display_errors());
        $this->load->view('display', $error);  //loads the view display.php with error
    }
    else
    {
      echo 'success!';
    }
    echo 'end upload';
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('display', $data); //loads view display.php with data
  }

}
4
  • the same result.. after submit, it only displays start upload and nothing after that..
    – dapidmini
    Commented Dec 27, 2016 at 8:00
  • i think you have to set more $config[]..see here codeigniter.com/userguide3/libraries/file_uploading.html Commented Dec 27, 2016 at 8:05
  • also check write permission on your uploading folder...problem may be due to this... Commented Dec 27, 2016 at 8:13
  • I checked using is_writable and it's writable. tried removing !empty($_FILES) condition and still the same thing...
    – dapidmini
    Commented Dec 27, 2016 at 8:21
0

change from

$config['upload_path'] = './assets/img/tempfile/';

to

$config['upload_path'] = 'assets/img/tempfile/';
1
  • what is the different between './assets/img/tempfile/ and 'assets/img/tempfile/'?? Commented Dec 27, 2016 at 9:45
0

i check this code this work perfect simply add this line

 $config['allowed_types'] = 'png|gif|jpg|jpeg';
 $config['max_size']    = '7000';

after this

 $config['upload_path'] = './assets/img/tempfile/';

or you can also set this in view page

action="<?php echo base_url('YourController/testupload');?>"
0

this is due to php server version and it's option.

1).go to cpanel account

2).click "select php version", in the software section.

3).tap the "fileinfo" chexbox in the php option and save.

now you can upload file perfectly.

0

Same issue as described. I have fixed it using the following: Open the file system/libraries/Upload.php go to function validate_upload_path() and add the command return TRUE; as a last line inside this function. Save and try again.

0

use this for image upload:

  $valid_extensions = array('jpeg', 'jpg', 'png');
  if ($_FILES['filename']['error'] == 0) {

        $img = $_FILES['filename']['name'];
        $tmp = $_FILES['filename']['tmp_name'];
        $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
         if (in_array($ext, $valid_extensions)) {
            $path = "./assets/img/tempfile/" . strtolower($img);
            if (move_uploaded_file($tmp, $path)) {

                $_POST['filename'] = $path;

            }
        }
    }

use insert query to insert file :

       $this->db->insert('table_name', $_POST);
0

Having the same problem on macOS. It seems that if you are not the "main" user of your laptop/pc, the default permission is "Read Only". You must change it to "Read & Write".

  1. Right click on the folder
  2. Get Info
  3. Sharing and permissions
  4. Change 'Read Only' to 'Read & Write'
0

add allowed file type parameter in config

$config['allowed_types'] = 'gif|jpg|png';
0

your code is missing the following,

    $this->load->library('upload',$config);

Recorrect it by rewriting the above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.