codeigniter个人资料图片上传

时间:2019-09-08 13:26:41

标签: php html codeigniter upload

我正在尝试上传用户的个人资料头像。我想做的是将用户图片上传到/ assets,并将路径存储在我的表“用户”中的“头像”字段下。因此,我阅读了在线codeigniter教程,但无法弄清某些方面。首先,在我看来,该表单用于上传图片:

 <div class="profile-img">
                        <img class = "rounded-corners" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS52y5aInsxSm31CvHOFHWujqUx_wWTS9iM6s7BAm21oEN_RiGoog" alt="profile image"/>
                        <div class="file btn btn-lg btn-primary">
                        Change photo
                        <?php echo form_open_multipart('pages/do_upload');?>
                            <input type="file" name="avatar"/>
                            <input type="submit" value="upload"/>
                        </div>
                    </div>

,然后在codeigniter教程中创建了控制器:

public function do_upload()
{
    $config['upload_path'] = './assets';
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = 100;
    $config['max_width'] = 1024; 
    $config['max_height'] = 768;

    $this->load->library('upload', $config); 

    $data = array('upload_data' => $this->upload->data());

    $this->load->view('profie', $data);

}

但是我什至无法上传图片到文件夹中……而且,我必须考虑一个好主意,将我要上传的图片重命名并将路径存储在列的表中……但是我d想至少上传图片开始

个人资料视图:

<div class="container emp-profile">
        <form method="post">
            <div class="row">
                <div class="col-md-4">
                    <div class="profile-img">
                        <img class = "rounded-corners" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS52y5aInsxSm31CvHOFHWujqUx_wWTS9iM6s7BAm21oEN_RiGoog" alt="profile image"/>
                        <div class="file btn btn-lg btn-primary">
                        Change photo
                        <?php echo form_open_multipart('pages/do_upload');?>
                            <input type="file" name="avatar"/>
                            <input type="submit" value="upload"/>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="profile-head">
                                <h6>
                                Hi <?php echo($user->username) ?>
                                </h6>
                        <ul class="nav nav-tabs" id="myTab" role="tablist">
                            <li class="nav-item">
                                <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Profile Details</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Your Vehicles</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="profile-work">
                    <!-- here you can add some more info, but the database needs to be upgraded for that-->
                    </div>
                </div>
                <div class="col-md-8">
                    <div class="tab-content profile-tab" id="myTabContent">
                        <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <label>Username</label>
                                        </div>
                                        <div class="col-md-6">
                                            <p><?php echo($user->username) ?></p>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <label>Email</label>
                                        </div>
                                        <div class="col-md-6">
                                            <p><?php echo($user->email) ?></p>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-6">
                                            <label>Member since</label>
                                        </div>
                                        <div class="col-md-6">
                                            <p><?php echo($user->created_at) ?></p>
                                        </div>
                                    </div>
                        </div>

1 个答案:

答案 0 :(得分:0)

@TimBrownlaw 我再次检查了我的代码和所有内容...现在这是我的观点:                                  更改照片                                                                                                                              这是我的控制器:

public function do_upload()
{
    $config['upload_path'] = 'uploads/';
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = 10000;
    $config['max_width'] = 1024; 
    $config['max_height'] = 768;

    $this->load->library('upload', $config); 

    $data = array('upload_data' => $this->upload->data());
    var_dump($data);
    $this->load->view('profile', $data);

 }

现在我收到此消息:“上载路径似乎无效。”但是根据我的计算机,我有权限...这可能是与docker或lamp服务器有关的问题吗? 我认为这与写权限有关,因为这里的这段代码一直在说我的目录不可写。

<?php if(is_writable(base_url('uploads'))){
echo 'writable';
}
 else { echo  (base_url('uploads'). ' ' ."is not writable");} ?>

我显然尝试过chmod 777“目录”,并且根据我的mac,我获得了读写许可。但是无法上传该死的照片。可以和码头工人的灯箱设置相关吗?