【问题标题】:Why can i not upload images to my folder anymore?为什么我不能再将图像上传到我的文件夹?
【发布时间】:2012-11-21 11:18:31
【问题描述】:

这是我几周前所做的工作,但在我对视图文件进行了一些更改后,现在不再将图像保存到我的 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>  

非常感谢您的帮助

【问题讨论】:

  • 当我选择一个文件并点击上传时 - 如果我在没有选择文件的情况下点击按钮,它会返回错误。我实际上在一个阶段完成了这项工作
  • 看起来我已经在网上尝试了一些示例,但我不得不重新开始,因为我想让它像以前一样工作,但没有运气 - 我不知道我哪里出错了。
  • 您从array('error'=&gt;$this-&gt;upload-&gt;display_errors()) 得到的确切错误是什么??
  • 它会生成库上传错误您没有选择要上传的文件 - 它可能会退回此错误,因为似乎没有文件上传到文件夹中并且图像未显示在屏幕上任何一个。但困扰我的是文件上传后没有出现在文件夹中

标签: php codeigniter


【解决方案1】:

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

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

$this-&gt;upload-&gt;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')

【讨论】:

  • 感谢文件正在上传到文件夹,但是我上传图片时怎么没有出现?我试过说 " width='300' height='300'/> 但是尽管在我的控制器中声明了 $data,但我得到了一个未定义的变量错误消息。
  • 我有answered your other question,希望对您有所帮助。如果您对this问题有任何帮助,或者您是否满意并且上传正常,并且您现在了解问题所在,请告诉我。
  • 嗨,韦尔斯利 - 我现在才看到这条评论 - 图像路径现在已正确保存到我的个人资料图像数据库中,并且图像首先在我的视图中上传到屏幕,但是当我刷新图像时消失了。我在想这是否与我的 getProfileImage 函数有关 - 你可能会看到一个稍后我提到过的问题 - 但正如我所说的那样,我仍然有这个问题 - 如果你能贡献任何东西,我会非常感激
猜你喜欢
  • 2016-07-27
  • 2018-12-19
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 2016-12-13
  • 2020-02-06
  • 2011-11-27
  • 2019-11-15
相关资源
最近更新 更多