为什么我不能再将图像上传到我的文件夹了?

时间:2012-11-20 22:56:15

标签: php codeigniter

这是我几周前工作的东西,但是在我对视图文件进行了一些更改后,图像现在不再保存到我的assets / uploads文件夹中。我一直收到错误 - 你没有选择要上传的文件。尽管确保路径绝对正确。我在这做错了什么?

这是我的控制器:

<?php
class HomeProfile extends CI_Controller 
{


function HomeProfile()
{
parent::__construct();
$this->load->model("profiles");
$this->load->model("profileimages");
$this->load->helper(array('form', 'url'));
 }



 function upload()
      {
    $config['path'] = './web-project-jb/assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '10000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

    $this->load->library('upload', $config);
    $img = $this->session->userdata('img');
        $username = $this->session->userdata('username');
    $this->profileimages->putProfileImage($username, $this->input->post("profileimage"));
//fail show upload form
    if (! $this->upload->do_upload())
{

    $error = array('error'=>$this->upload->display_errors());

    $username = $this->session->userdata('username');


    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);

    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('homeprofile/upload_fail', $error);
    $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
    $this->load->view('shared/footer');

    //redirect('homeprofile/index');

}

else
{
    //successful upload so save to database


    $file_data = $this->upload->data();


    $data['img'] = base_url().'./web-project-jb/assets/uploads/'.$file_data['file_name'];
    // you may want to delete the image from the server after saving it to db
    // check to make sure $data['full_path'] is a valid path
    // get upload_sucess.php from link above
    //$image = chunk_split( base64_encode( file_get_contents( $data['file_name'] ) ) );



    $this->username = $this->session->userdata('username');

    $data['profileimages'] = $this->profileimages->getProfileImage($username);


    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);

    $username = $this->session->userdata('username');


}

    }



  function index()
  {

$username = $this->session->userdata('username');

$data['profileimages'] = $this->profileimages->getProfileImage($username);

$viewData['username'] = $username;
$viewData['profileText'] = $this->profiles->getProfileText($username);

$this->load->view('shared/header');
$this->load->view('homeprofile/homeprofiletitle', $viewData);
$this->load->view('shared/nav');
//$this->load->view('homeprofile/upload_form', $data);
$this->load->view('homeprofile/homeprofileview', $data, $viewData, array('error' => ' ' ) );
$this->load->view('shared/footer');
   }

   }

以下是我的观点:

<div id="maincontent">
<div id="primary"> 
<?//=$error;?>
 <?//=$img;?> 
 <h3><?="Profile Image"?></h3>
  <img src="<?php echo'$img'?>" width='300' height='300'/>
   <?=form_open_multipart('homeprofile/upload');?>
    <input type="file" name="img" value=""/>
    <?=form_submit('submit', 'upload')?>
    <?=form_close();?> 
    <?php if (isset($error)) echo $error;?>
  </div> 
</div>  

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

Codeigniter上传类说“您没有选择要上传的文件”,因为它正在寻找userfile数组中的密钥$_FILES

来自文档:http://codeigniter.com/user_guide/libraries/file_uploading.html

  

$this->upload->do_upload()

     

根据您设置的偏好执行上传。注意:通过   默认情况下,上传例程期望文件来自表单字段   名为 userfile ...如果您想设置自己的字段名称   只需将其值传递给do_upload函数:

$field_name = "some_field_name";
$this->upload->do_upload($field_name)

所以你有两个选择,改变

<input type="file" name="img">

<input type="file" name="userfile">

...或更改

$this->upload->do_upload()

$this->upload->do_upload('img')