【问题标题】:Uploading file and display its name as a post上传文件并将其名称显示为帖子
【发布时间】:2014-02-24 05:51:46
【问题描述】:

我正在尝试上传文件并将其名称替换为标题名称,但我无法通过控制器中的 echo 获取文件名,即使不是作为分析器中的 POST

我还需要重命名它,但在此之前我需要知道发布值,即文件名。

我在这里发布我的代码。

我的看法

<?php echo form_open_multipart('emailtemplate/do_upload');?>

<tr class='odd gradeX'>
    <td class='center'>
        <input type="file" name="userfile" size="20" />
    </td>
    <td class='center'>
        <input placeholder='Title For Your File' name='title' class='form-control'>
    <td class='center'>
        <input placeholder='Comment On File' name='comment' class='form-control'>
    </td>
</tr>

<input type="submit" value="upload" />

</form>

我的控制器

function do_upload()
{
    $this->load->helper('form');
    $this->load->helper('html');
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['encrypt_name']     = true;
    //$file=$this->input->post('userfile');
    $this->load->library('upload', $config);
    $this->upload->data();
    $file_name = $this->upload->do_upload('userfile');
    echo $file_name;


    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('add/addfile', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('add/addfile', $data);
    }
    $this->output->enable_profiler(true);
}

【问题讨论】:

  • 在 codeIgniter 中通过 $this-&gt;input-&gt;post(); 访问 post 值,与 $_POST 相同
  • 上传的文件如何重命名??

标签: php codeigniter file-upload


【解决方案1】:

使用 POST 值重命名上传的文件:

//get extension 
  $ext = end(explode(".", $_FILES["userfile"]['name']));
  $config['file_name'] = $this->input->post("title").'.'.$ext;

删除$config['encrypt_name'] = true;。否则你的文件名将被加密。

确保您的title 不存在于您上传的文件夹中。或者在标题后面附加一些随机数/字符。

【讨论】:

    【解决方案2】:

    我已经创建了一个上传文件的函数:-

    它需要参数,即 field_name

        public function fileUpload($field) {
        //get file extension
        $exts = @split("[/\\.]", $_Files[$field][name]);
        $n = count($exts) - 1;
        $ext = $exts[$n];
        //create image name  or replace it with your desired name
        $imageName = date("Ymdhis") . time() . rand();
        $config['file_name'] = $imageName; //set the desired file name
        $config['overwrite'] = true;        
        $config['allowed_types'] = str_replace(',', "|", $ext);
        $config['upload_path'] = ABSOLUTEPATH . 'product/original/';
        $config['optional'] = true;
        $config['max_size'] = '1000';
        $config['max_width'] = '00';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($field, true)) {
            $error = array('error' => $this->upload->display_errors());
            $r = $error;
        } else {
            $data = array('upload_data' => $this->upload->data());
            $r = "sucess";
        }        
        return $r;
    }
    

    【讨论】:

      【解决方案3】:

      下面我写的代码对你有帮助

      $config['upload_path'] = './uploads/path';
              $config['allowed_types'] = 'png|jpg|jpeg|gif';
              $config['max_size']    = '1000000';
              $config['max_width']  = '1000000';
              $config['max_height']  = '1000000000';
      
              $this->load->library('upload', $config);
              $files = $_FILES;
      
              if(isset($files['file']['name']))
              {   
                  $filename = $files['file']['name'];
                  $ext = end(explode(".", $filename));
                  $_FILES['file']['name']=  date('d').date('m').date('y').'_'.date(time()).'.'.$ext; //here i am changing file name with current date as your wish to change your logic   
      
              //  print_r($_FILES);exit; check the changed name
      
                 if ($this->upload->do_upload('file')) 
                   {
                    $upload = array('upload_data' => $this->upload->data());
                    $imagename = $upload['upload_data']['file_name']; //uploaded your image name
                   }
                   else
                   {
                          $error = array('error' => $this->upload->display_errors());
                          print_r($error); //error 
                   }
              }
      

      【讨论】:

        【解决方案4】:

        注意:嘿 user3345331 ,请参阅该行

        $this->upload->do_upload() 
        

        用这个替换

        $this->upload->do_upload('userfile');
        

        此用户文件是您的 HTML 代码中具有文件类型的输入框的 ID。

        【讨论】:

          猜你喜欢
          • 2014-11-26
          • 1970-01-01
          • 1970-01-01
          • 2015-12-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-21
          • 2021-07-17
          相关资源
          最近更新 更多