【问题标题】:Codeigniter Rename file on uploadCodeigniter 在上传时重命名文件
【发布时间】:2014-03-15 17:27:30
【问题描述】:

我正在尝试在上传时添加时间作为图像名称的前缀以及原始名称,但我无法弄清楚。请帮助我使用以下代码在上传时为我的原始文件名添加前缀。

<?php

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {


        $config['upload_path'] = 'Public/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';



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


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

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

            $this->load->view('upload_success', $data);
        }
    }
}
?>

【问题讨论】:

标签: php codeigniter file-upload


【解决方案1】:

您可以使用 CI 原生选项加密文件名:

$config['encrypt_name'] = TRUE;

你可以用你自己的代码来做:

$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;

【讨论】:

    【解决方案2】:

    由于某些原因,对 do_upload 函数的连续调用不起作用。它坚持第一个函数调用设置的第一个文件名

    $small_photo_url  = $this->upload_photo('picture_small',  $this->next_id.'_small ');
    $medium_photo_url = $this->upload_photo('picture_medium', $this->next_id.'_medium');
    $large_photo_url  = $this->upload_photo('picture_large',  $this->next_id.'_large ');
    

    文件名都是“00001_small”、“00001_small1”、“00001_small2” 给定以下配置

    function upload_photo($field_name, $filename)
    {
        $config['upload_path'] = 'Public/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $config['file_name'] = $filename;
    
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload())...
    

    我认为这是因为该行在您第二次调用时不起作用。它不会再次设置配置

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

    ================================================ ============================ 连续调用do_upload函数时遇到的问题解决方法:

    // re-initialize upload library
    $this->upload->initialize($config);
    

    【讨论】:

    • 我遇到了同样的问题...你有没有找到任何解决方案???
    • 找到了解决这个问题的办法。如果有人在寻找解决方案,我会放在这里。解决方法很简单...在构造函数中加载库$this-&gt;load-&gt;library('upload');然后每次上传前初始化函数,$this-&gt;upload-&gt;initialize($config);就可以了...
    • 我遇到了这个问题,并通过您的解决方案解决了。谢谢!
    【解决方案3】:

    对于您正在寻找的东西,这对我有用:

    $path = $_FILES['image']['name'];
    $newName = "<Whatever name>".".".pathinfo($path, PATHINFO_EXTENSION); 
    

    【讨论】:

      【解决方案4】:
              /* Conf */
              $new_name                   = time().$_FILES["userfiles"]['name'];
              $config['file_name']        = $new_name;
              $config['upload_path']      = './upload/';
              $config['allowed_types']    = 'gif|jpg|png';
              $config['max_size']         = '';
              $config['max_width']        = '';
              $config['max_height']       = '';
              $config['file_ext_tolower'] = TRUE;
      
            /*  Load the upload library  */   
              $this->load->library('upload', $config);
      

      【讨论】:

        【解决方案5】:

        当您使用 do_upload() 函数时,如果已存在则重命名文件名并上传文件

        系统->库->Upload.php

        do_upload() 的默认函数返回 true 或 false 替换

        return $this->upload_path.$this->file_name;

        在另一个文件中

         <input type='file' name='userfile'/>
        
        
        <?php
            $upload_file_name = $this->upload->do_upload('userfile');
                        $finalname;
                        if (!$upload_file_name) {
                            $finalname = 'default.png';
                        } else {
        
                            $split_data_upload_file_name = explode("/", $upload_file_name);
                            foreach ($split_data_upload_file_name as $i => $value) {
        
                                $finalname = $value;
                            }
                        }
        
        ?>
        

        【讨论】:

          【解决方案6】:
          $config['file_name'] = $new_name;
          

          只需按照配置代码添加即可。

          【讨论】:

            【解决方案7】:

            这应该是在上传之后。

            在这里处理你想要的文件名:

            $a=$this->upload->data();         
            rename($a['full_path'],$a['file_path'].'file_name_you_want');
            

            【讨论】:

              【解决方案8】:

              试试这个:

              $config['file_name'] = "yourCustomFileName";
              

              【讨论】:

                【解决方案9】:

                如果您的$config 使用此代码:$config['encrypt_name'] = TRUE; 您的文件名将被强制使用加密名称重命名,只需删除代码即可。

                【讨论】:

                  【解决方案10】:

                  解决方案 1: 使用新的自定义名称和特定名称上传文件(注意:当您提交记录的唯一名称时,此答案适用,例如学生姓名是记录中的唯一值那么你将使用$_POST['student_name'])

                  $config['file_name'] = str_replace(' ', '_', $_POST['anyUniqueValue']);
                  //any Other unique value that you are using to submit with this file
                  $config['upload_path'] = './folderName/';
                  $config['encrypt_name'] = FALSE;
                  

                  解决方案 2: 使用新的唯一名称上传文件

                  $config['file_name'] = time();
                  $config['upload_path'] = './folderName/';
                  $config['encrypt_name'] = FALSE;
                  

                  解决方案 3: 使用 codeigniter 的默认选项上传具有新唯一名称的文件

                  $config['encrypt_name'] = TRUE;
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-06-24
                    • 2017-03-19
                    • 2014-01-15
                    相关资源
                    最近更新 更多