0

我正在创建一个个人资料页面,当我选择要上传的图像时,它将存储在上传文件夹中,但不会显示在屏幕上 - 为什么会这样。

图像的容器似乎出现了,但不是图像

但是如果我不选择要上传的图像,上传功能会正确返回错误

这是我的控制器,其中包含上传功能 - 我意识到该文件夹​​称为 puploads:

 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 = array(

        'allowed_types' =>'gif|jpg|jpeg|png',
        'upload_path' =>'./web-project-jb/assets/puploads/',
         'max_size' => 10000,
         'max_width' => 1024,
        '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/homeprofileview', $error, $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/puploads/'.$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');

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


    //redirect('homeprofile/index');
        }

   }



     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');
  }

  }

这是我的个人资料图像模型:

function ProfileImages()
{
    parent::__construct();

}

function exists($username)
{
    $this->db->select('*')->from("profileimages")->where('user', $username);
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {

        return true;
        /*
         echo "user $user exists!";
        $row = $query->row();
        echo " and his profileimage is $row->profileimage";
        */
    }

    else

    {

        return false;
        //echo "no such user as $user!";
    }

}


function putProfileImage($username, $img)
{


    $record = array('user' => $username, 'profileimage' => $img);
    if ($this->exists($username))
    {
        $this->db->where('user', $username)->update('profileimages', /*'./uploads/.' */$record);

        //appends
    }
    else
    {
        $this->db->where('user', $username)->insert('profileimages', $record);

    }

}

function getProfileImage($username)
{
    $this->db->select('*')->from('profileimages')->where('user', $username);
    $query = $this->db->get();
    if ($query->num_rows() > 0){
        $row = $query->row();
        return $row->profileimage;
    }

    return Null;


}

}

这是我的看法:

<h3><?="Profile Image"?></h3>
 <img src="./web-project-jb/assets/puploads/" width='300' height='300'/>
  <?=form_open_multipart('homeprofile/upload');?>
    <input type="file" name="userfile" value=""/>
    <?=form_submit('submit', 'upload')?>
    <?=form_close();?> 
    <?php if (isset($error)) echo $error;?>
   </div>
  </div>

我试过用 img src 替换,但我只是得到一个错误,说数据无法识别

4

1 回答 1

0

您的视图文件不表示任何输出动态图像的尝试,您已经src硬编码并且根本没有使用您的$img变量(您在控制器中设置的变量$data['img']):

$data['img'] = base_url().'./web-project-jb/assets/puploads/'.$file_data['file_name'];

在这里,您似乎也在混合服务器路径和 URL。假设您的上传文件夹的 URL 是:

http://example.com/web-project-jb/assets/puploads/

...在您的控制器中使用它(上传成功后):

$data['img'] = 'web-project-jb/assets/puploads/'.$file_data['file_name'];

那么在你看来:

<img src="<?php echo base_url($img); ?>" width='300' height='300'/>
于 2012-11-23T18:21:59.020 回答