Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

php - Codeigniter cannot upload image

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
";
        }
        else{
            //Error. this->upload->display_errors();
        }
    }
    header("Location: http://localhost:82/MyCRM/admin/#/add_content");
 }
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...