【问题标题】:Rename File Name During Uploading上传时重命名文件名
【发布时间】:2016-06-08 06:17:04
【问题描述】:
$uniqId = $this->input->post('U_I_T_Roll_No');
$config['upload_path'] = "./uploads/ProfileImages/";
        $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
        $config['max_size']  = '1024';
        $config['max_width']  = '1000';
        $config['max_height']  = '1000';
        /* Load the upload library */
        $this->load->library('upload', $config);
        /* Create the config for image library */
        /* (pretty self-explanatory) */
        $configThumb = array();
        $configThumb['image_library'] = 'gd2';
        $configThumb['source_image'] = '';
        $configThumb['create_thumb'] = FALSE;
        $configThumb['maintain_ratio'] = TRUE;
        /* Set the height and width or thumbs */
        /* Do not worry - CI is pretty smart in resizing */
        /* It will create the largest thumb that can fit in those dimensions */
        /* Thumbs will be saved in same upload dir but with a _thumb suffix */
        /* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
        $configThumb['width'] = 400;
        $configThumb['height'] = 400;
        /* Load the image library */
        $this->load->library('image_lib');

  /* We have 5 files to upload
   * If you want more - change the 6 below as needed
   */
  for($i = 1; $i < 5; $i++) {
    /* Handle the file upload */
    $upload = $this->upload->do_upload('image'.$i);
    /* File failed to upload - continue */
    if($upload === FALSE) continue;
    /* Get the data about the file */
    $data = $this->upload->data();

    $uploadedFiles[$i] = $data;
    /* If the file is an image - create a thumbnail */
    if($data['is_image'] == 1) {
        $configThumb['source_image'] = $data['full_path'];
        $this->image_lib->initialize($configThumb);
        $this->image_lib->resize();
    }
  }
  $S_image = $_FILES['image1']['name'];
  $F_image = $_FILES['image2']['name'];
  $M_image = $_FILES['image3']['name'];
  $LG_image = $_FILES['image4']['name'];
  $data5=array (
    'Uniq_Id' => $this->input->post('U_I_T_Roll_No'),

    'S_image' => $S_image,
    'F_image' => $F_image,
    'M_image' => $M_image,
    'LG_unique1' => $LG_image,
    );


  $this->InsertData->studentimageupload($data5);

上面的代码工作正常。我有 4 个要上传和正确上传的图像以及保存到数据库中的图像名称。问题是。

我想根据我的上传图片名称并将图片名称保存到数据库中。

点赞: S$uniqId、F$uniqId、M$uniqId、LG$uniqId

【问题讨论】:

    标签: php image codeigniter image-uploading


    【解决方案1】:

    $this-&gt;load-&gt;library('upload', $config);之前做这样的事情

    $this-&gt;load-&gt;library('upload', $config); 更改为$this-&gt;load-&gt;library('upload'); 并见下文

     for($i = 1; $i < 5; $i++) {
        $img=$_FILES['image'.$i]['name'];
        $new_name=NEW_NAME;
        $ext = strtolower(substr($img, strpos($img,'.'), strlen($img)-1));
        $file_name=$new_name.$ext;
        $config['file_name'] = $file_name;
    $this->upload->initialize($config);
    /* Handle the file upload */
    $upload = $this->upload->do_upload('image'.$i);
    /* File failed to upload - continue */
    if($upload === FALSE) continue;
    /* Get the data about the file */
    $data = $this->upload->data();
    
    $uploadedFiles[$i] = $data;
    /* If the file is an image - create a thumbnail */
    if($data['is_image'] == 1) {
        $configThumb['source_image'] = $data['full_path'];
        $this->image_lib->initialize($configThumb);
        $this->image_lib->resize();
        }
      }
    

    【讨论】:

    • 实际上我有 4 张图片要上传。你能把它放在数组中吗..以及如何命名所有五张图片..你只为一张图片上传编写了代码。我必须写e次..
    • 对所有 5 个执行相同操作或创建一个函数。
    【解决方案2】:

    使用的名称是 $_FILES['image1']['name'] 中的任何名称。如果要更改它,可以在上传表单本身中更改它。如果这不是可取的,您必须致电rename() 然后更新

    $S_image = $_FILES['image1']['name'];
    $F_image = $_FILES['image2']['name'];
    $M_image = $_FILES['image3']['name'];
    $LG_image = $_FILES['image4']['name'];
    

    在存储到数据库之前,将文件重命名为任何内容。

    【讨论】:

      【解决方案3】:

      我认为您可以通过在每次调用 do_upload 方法时初始化配置来解决重命名问题。

      $config['file_name'] = YOUR_OWN_NAME

      然后在你的 for 循环中初始化,就在你的 do_upload 调用之前

      $this-&gt;upload-&gt;initialize($config);

      【讨论】:

      • 你能编辑代码并告诉我热门的事情吗?有一个 for 循环,我无法理解如何重命名多个图像。
      【解决方案4】:

      您应该能够在加载库后和上传文件之前指定“文件名”..

      //make your config array.
      $this->load->library('upload', $config);
      
      for($i=1; $i < 5; $i++)
      {
          $this->upload->set_filename($path, $filename);
          $upload = $this->upload->do_upload('image'. $i);
          //do whatever you want to do with the file.
      }
      

      到目前为止,我还没有在我的任何项目中使用它,但它应该会产生预期的结果。请告诉我,如果它不起作用..

      【讨论】:

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