0

我已经对这个问题提出了一些问题,但我最终并没有真​​正取得太大进展。我正在尝试将图像上传到页面上,并让文件名出现在用户名旁边的数据库中,但它一直显示为零。为什么会这样?图像字段是一个 varchar,因此它将保存文件名。但既然我希望我的 getProfileImage 函数将图像显示回屏幕,它应该是一个 blob 吗?

这是我的控制器:

 function __construct()
  {
   // Call the parent construct
   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');
//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, which it doesn't


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


    $data['img'] = '/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');

    $this->profileimages->putProfileImage($username, $img);


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


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

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

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

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

}

我的模型:

var $proimage_path;


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

    $this->proimage_path = 'web-project-jb/assets/puploads';

}




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', $record);


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

    }

}

function getProfileImage($img)
{

$this->db->select('*')->from('profileimages')->where('profileimage', $img);

$query = $this->db->get();
if ($query->num_rows() > 0){
    $row = $query->row();
    return $row->profileimage;
  }

  return $img;


 }
}
4

2 回答 2

0

你能告诉我为什么你从会话中读取文件名而不是使用上传的图像文件名吗?假设用户没有图像并且您在用户登录时设置了会话,那不是空吗?

于 2012-11-27T01:15:58.750 回答
0

在你的控制器中

function upload() {
    if ($this->input->post('submit')) {
        $config['upload_path'] = './uploads/album/';
        $config['allowed_types'] = 'jpeg|jpg|png|gif|';
        $config['max_size'] = '500000';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload()) {
            $file = $this->upload->data();
            $keterangan = $this->input->post('keterangan');
            $this->MGallery->addGallery($file['file_name'], $keterangan);
            $this->session->set_flashdata('message', 'Foto <b>' . $file['file_name'] . '</b> berhasil di upload!');
            redirect('admin/album/index', 'refresh');
        } else {
            $this->session->set_flashdata('message', 'Foto <b>' . $file['file_name'] . '</b> gagal di upload!');
            redirect('admin/album/lihat', 'refresh');
        }
    } else {
        redirect('admin/album/lihat', 'refresh');
    }
}

在你的模型中

function addGallery($file_name, $keterangan) {
    $now = date("Y-m-d H:i:s");
    $data = array(
        'foto' => $file_name,
        'keterangan' => $keterangan,
        'IDKategori' => $this->input->post('kategori'),
        'tgl' => $now
    );
    $this->db->insert('album', $data);
}

然后,在你看来,像这样

<?php
if ($this->session->flashdata('message')) {
    echo "<div class='message'>" . $this->session->flashdata('message') . "</div>";
} ?>
<form action="<?= base_url(); ?>admin/album/upload" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <p>
        <input type="file" name="userfile" size="20" />
    </p>
    <p>
        <select name="kategori">
        <?php
        foreach ($gallery_cat as $key => $list) {
        ?>
            <option value="<?= $list['IDKategori'] ?>">
            <?= $list['kategori'] ?>
        </option>
        <?php
        }
        ?>
    </select>
</p>
<p>
    <textarea name="keterangan" cols="40" rows="5"></textarea>
</p>
<p>
    <label>
        <input type="submit" name="submit" value="Uplaod Foto" />
    </label>
    <input type="button" value="Kembali" onClick="javascript: history.go(-1)" />
</p>

于 2012-11-27T04:21:48.890 回答