个人资料图片将上传到文件夹,但不会屏幕 [英] Profile Image will upload to folder but not to screen

查看:67
本文介绍了个人资料图片将上传到文件夹,但不会屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建个人资料页面,当我选择要上传的图片时,该图片将存储在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 '=> $ 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
{
//上传成功,保存到数据库


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


$ data ['img'] = base_url()。'/ web-project-jb / assets / puploads /'.$ file_data ['file_name'];
//你可能希望在将图像保存到db之后从服务器中删除该图像
//检查以确保$ data ['full_path']是一个有效的路径
// get upload_sucess .php从上面的链接
// $ 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');
}

}



函数索引()
{

$ 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模型: / p>



  function ProfileImages()
{
parent :: __构造();

}

函数存在($ username)
{
$ this-> db-> select('*') - > from(profileimages) - >其中('user',$ username);
$ query = $ this-> db-> get();

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

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

else

{

return false;
// echono such user as $ user!;
}

}


function putProfileImage($ username,$ img)
{


$ record = array('user'=> $ username,'profileimage'=> $ img);
if($ this-> exists($ username))
{
$ this-> db-> where('user',$ username) 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><?=个人资料图片?>< / h3& 
< img src =./ web-project-jb / assets / puploads /width ='300'height ='300'/>
<?= form_open_multipart('homeprofile / upload');?>
< input type =filename =userfilevalue =/>
<?= form_submit('submit','upload')?>
<?= form_close();?>
<?php if(isset($ error))echo $ error;?>
< / div>
< / div>

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


解决方案

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

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

这里也似乎是混合服务器路径和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'/> 


I'm creating a profile page where when I select an image to upload it will be stored in the uploads folder but will not display to the screen - why would this be.

The container of an image seems to appear, but not the image

But the upload function does correctly thtow back an error if I don't select an image to upload

Here is my controller with the upload feature within it - i realise the folder is called 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');
  }

  }

Here is my profileimages model:

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;


}

}

Here is my view:

<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>

I have tried replacing the img src with but I just get an error back saying data is unrecognised

解决方案

Your view file does not indicate any attempt to output a dynamic image, you have the src hardcoded and aren't using your $img variable at all (the one you set in the controller with $data['img']):

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

Here also you seem to be mixing server paths and URLs. Assuming the URL to your uploads folder is:

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

...use this in your controller (after the upload succeeds):

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

Then in your view:

<img src="<?php echo base_url($img); ?>" width='300' height='300'/>

这篇关于个人资料图片将上传到文件夹,但不会屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆