2

I've been googling around for 2-3 hours looking at every stackoverflow thread related to this. As far as I can see, lots of people share my frustration. Like many others, I cannot upload an image, even though it shows no errors.

I've literally tried anything and everything I found online, but nothing seems to work...

I can only see the properties of the image with var_dump($_FILES). I want to store the image into a folder and its name into a database.

Controller

function upload_sl()
{ 
    $config['upload_path'] = 'sliki/samurajsliki/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';

$this->load->library('upload', $config);
/*$this->upload->initialize($config);*/
/*var_dump($_FILES);*/
if ( ! $this->upload->do_upload())
{
  $error = array('error' => $this->upload->display_errors());

  $this->load->view('samuraj/slikaclen', $error);
}
else
{
  $data = array('upload_data' => $this->upload->data());

  $this->load->view('header_samurajmain');
  $this->load->view('samuraj/admin_samuraj');
  $this->load->view('footer_subpage');
}
}

View

<?php echo $error;?>
                   <div class="center-block" style="width: 30%; height: 50%; clear:both;">
                   <?php echo form_open_multipart('admin_page/upload_sl');?>
                    <input type="file" name="userfile" size="20" />

                    <br />

                    <input type="submit" value="Стави нова слика" />
                    </form>
                     </div>
3
  • 2
    Your upload path does not seem right. It should be a full absolute server path... such as on shared hosting, it would be something like home/account_name/www/my/path/to/folder Commented Feb 2, 2017 at 19:53
  • 1
    it is most likely an issue with your path. check this: stackoverflow.com/a/41604500/2275490 Commented Feb 2, 2017 at 23:48
  • There was no error -_- just some server lag Commented Feb 3, 2017 at 15:12

3 Answers 3

1

I found this in my archive. I don't know if its working. Maybe you want to try it.

<form action="admin/#/add_content/do_upload" enctype="multipart/form-data" method="post" accept-charset="utf-8">
      <input type="file" class="mb5" name="userfiles[]" multiple>
</form>

Here is Controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload extends CI_Controller {

public function __construct(){
    parent::__construct();
}

public function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';

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

    $files = $_FILES;
    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    

        if($this->upload->do_upload()){
            echo "File ".$_FILES["userfile"]["name"]. " has been uploaded.\n";
        }
        else{
            //Error. this->upload->display_errors();
        }
    }
    header("Location: http://localhost:82/MyCRM/admin/#/add_content");
 }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I use form_open_multipart() from form helper or in action="<?php echo base_url('controller/upload_sl')"
1

Using the library Codeigniter ( library upload single file and multi file ) Easily upload .

Comments

0

There is nothing wrong with the code. There was server lag and the folder where the images go wasn't refreshing on its own. Sorry for wasting everyone's time - I did check all of your answers.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.