图像上传到codeigniter中的子文件夹

时间:2013-08-27 12:21:40

标签: php codeigniter image-uploading

我是codeigniter的新手。请帮我上传codeigniter.first中的图片我需要在“codeigniter / userimage /”中创建一个子文件夹,其中用户ID为文件夹名称。我做到了。这是我的控制器功能

function update(){
    $uppath="/var/www/html/codeigniter/userimage/";
    $sess_id=  $this->session->userdata('userid');
    $folder=$uppath.$sess_id;
    if(!is_dir($folder))
    mkdir ($folder,777);

    if($x>0){
        $config['upload_path']='./userimage/'.$folder.'/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '9000';
        $config['max_width']  = '1024';
        $config['max_height']  = '1000';
        echo 'haii'; 
        $this->load->library('upload');
        $this->upload->initialize($config);
        echo 'heloooo'; 
        if (!$this->upload->do_upload('picfile')) {
            print_r( $this->upload->display_error() );die;
        }
        else {
            $image=$this->upload->do_upload('picfile');
            // echo 'uploaded';
            echo $image;
        }
    }
}

我正在从'profileinfo'视图中调用模型'home'中编写的方法更新。

我的观点profileinfo是:

`<?php
  $fn='';
 $ln='';
 $un='';
 $em='';
 $b='';
 $gr='';
 foreach ($query as $row){
 $fn=$row->firstname;
 $ln=$row->lastname;
  $un=$row->username;
  $em=$row->email;
 $b=$row->bday;
 $gr=$row->gender;
}
?>
 <html>
<head></head>
<body>
  <h1><u>My Profile Details</u></h1>
    <br><br><div id="viewdiv" align="center">

          <div align="right" style="font-weight: bold;font-size: 20px;"> 
      <a  href="home">Back</a></div>
       <?php echo form_open_multipart('home/update');?>
<br>
<table border="0" align="center">
    <tr class='spacerow'>
        <td>
            <h2>  Name:</h2>
        </td>
        <td><?php echo"<input type='text' name='first' value='$fn'>";?>
        <?php echo"<input type='text' name='last' value='$ln'>";?></td>
    </tr>
    <tr class='spacerow'>
        <td>
            <h2>   User Name:</h2>
        </td>
        <td>
             <?php echo"<input type='text' name='uname' value='$un'>";?>
        </td>
    </tr>
    <tr class='spacerow'>
        <td>
            <h2>   Date of birth:</h2>
        </td>
        <td>
           <?php echo"<input type='text' name='dobb' id='datepicker' value='$b'>";?> 
        </td>
    </tr>
    <tr class='spacerow'>
        <td>
            <h2>    Gender: </h2>
        </td>
        <?php
        if($gr=="male"){
            ?>
        <td>
            <?php echo"<h3>Male:<input type='radio' name='gen' value='male' checked>";?>
             <?php echo"Female:<input type='radio' name='gen' value='female' ></h3>";?>
        </td>
        <?php
                    }
                  else{  
        ?>
        <td>
             <?php echo"<h3>Male:<input type='radio' name='gen' value='male'>";?>
            <?php echo"Female:<input type='radio' name='gen' value='female' checked></h3>";?>
        </td>
        <?php
                  }
                  ?>
    </tr>

    <tr class='spacerow'>
        <td>
            <h2>    Email:</h2>
        </td>
        <td>
            <?php echo"<input type='text' name='mail' value='$em'>";?>
        </td>
    </tr>
     <tr class='spacerow'>
        <td>
            <h2> Profile Picture:</h2>
</td>
<td>
   <input type="file" name="picfile"> 
   <p>if you want to change your profile picture,browse here..</p>


</td>
    </tr>


    <tr>
    <td><br>
        <center> <input type="submit" name="update" value="update"></center>

    </td>
    <td>

    </td>   
    </tr>


</table>



    </div>    
</body>

` 子文件夹“$ folder”正在创建内部userimage,我想将上传的图像存储到该子文件夹。我的问题是图像没有上传。你可以看到我在加载上传库之前和之后放了两个echo语句。但我只得到输出“haii”..我不知道什么是问题..请任何人帮助我..谢谢..

1 个答案:

答案 0 :(得分:0)

首先使用:

$uppath='./codeigniter/userimage/';

你的文件字段名称是什么?如果它不是userfile,那么提供如下字段名称:

if ( ! $this->upload->do_upload('field_name'))

最后将这样的错误打印到debug:

print_r( $this->upload->display_error() );die;

修改

function update(){
    $uppath="/var/www/html/codeigniter/userimage/";
    $sess_id=  $this->session->userdata('userid');
    $folder=$uppath.$sess_id;
    if(!is_dir($folder))
    mkdir ($folder,777);

    if($x>0){
        $config['upload_path']      = './userimage/'.$sess_id.'/';
        $config['allowed_types']    = 'gif|jpg|png|jpeg';
        $config['max_size']         = '9000';
        $config['max_width']        = '1024';
        $config['max_height']       = '1000';
        echo 'haii'; 
        $this->load->library('upload', $config);
        //$this->upload->initialize($config);
        echo 'heloooo'; 
        if (!$this->upload->do_upload('picfile')) {
            print_r( $this->upload->display_errors() );die;
        }
        else {
            $image=$this->upload->do_upload('picfile');
            // echo 'uploaded';
            echo $image;
        }
    }
}