个人资料图片将上传到文件夹但不会上传到屏幕

时间:2012-11-23 17:04:07

标签: php codeigniter

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

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

但是如果我没有选择要上传的图像

,则上传功能会正确显示错误

这是我的控制器,其中包含上传功能 - 我意识到该文件夹​​名为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');
  }

  }

这是我的profileimages模型:

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替换了但是我只是回复说数据无法识别

1 个答案:

答案 0 :(得分:0)

您的视图文件未指示任何输出动态图片的尝试,您有src硬编码且根本没有使用$img变量(您在控制器中设置的$data['img']变量{ {1}}):

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

此处您似乎也在混合服务器路径和URL。假设您的uploads文件夹的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'/>