【问题标题】:Profile Image will upload to folder but not to screen个人资料图片将上传到文件夹但不会上传到屏幕
【发布时间】:2012-11-12 01:32:58
【问题描述】:

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

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

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

这是我的控制器,其中包含上传功能 - 我意识到该文件夹​​称为 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 替换为,但我只是收到一条错误消息,说数据无法识别

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您的视图文件并未表明任何输出动态图像的尝试,您已经硬编码了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'/>
    

    【讨论】:

    • 非常感谢,我现在可以显示它了...您建议我如何在我的模型中添加一个 appends 命令到我的 putprofileimage 函数,它将图像作为链接存储在我的数据库中?与显示为 Blob01 不同,这并不表示已进行任何更改?
    • 很高兴为您提供帮助。如果您有新问题,请使用 链接。确保在您的新问题中完全清楚,因为您刚才问我的内容并没有多大意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2014-04-14
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多