【问题标题】:Codeigniter image upload will only upload to one dirCodeigniter 图片上传只会上传到一个目录
【发布时间】:2011-05-30 12:16:39
【问题描述】:
function upload($path){
  $config['overwrite'] = TRUE;
  $config['allowed_types'] = 'jpg|jpeg|gif|png';
  $config['max_size'] = 2000;

  if($path =='profile'){
   $config['upload_path'] = '../assets/uploads/avatars';
   $config['file_name'] = $this->id;  
  }
  if($path =='company'){
   $config['upload_path'] = '../assets/uploads/company';
   $config['file_name'] = $this->id.'_company';
  }

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

  $image_data = $this->upload->data();
  if($path == 'profile'){
   $this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
  }
  if($path == 'company'){
   $this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
  }
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->upload_path,
   'maintain_ratio' => true,
   'width' => 500,
   'height' => 500
  );

  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
 }

这是我的上传功能,在$path = 'profile'时可以正常使用,但是在company时,它不会上传到“公司”文件夹... 有什么理由应该这样吗?!我在这里不知所措......这个功能在它进入头像文件夹时有效,但如果它进入公司文件夹......

【问题讨论】:

  • 替换 $this->load->library('image_lib', $config);通过 $this->load->library('image_lib'); $this->image_lib->初始化($config);

标签: php codeigniter upload


【解决方案1】:

我有多个文件夹的类似问题。我修复的方法是使用初始化函数,而不是将配置作为参数传递给加载库函数。

$this->upload->initialize($config);

您可以加载该库,然后设置您的配置并调用初始化方法。

希望这会有所帮助。

【讨论】:

  • 我什至不知道问题出在哪里...但我将其用于路径:realpath(APPPATH . '../assets/uploads/company');...
  • ifaour 提出了一些非常好的建议。我会确保你试试他说的。 CI 文档以这种方式设置路径:“./uploads”,其中点表示站点根目录。因此,假设您的站点位于 mysite.com,则上传目录位于 mysiste.com/uploads
【解决方案2】:

我认为所有建议都非常好,您还可以确保在表单中发布正确的数据: $this->upload->do_upload();默认情况下期望表单名称为 'userfile'

我也发现有时显示一些错误真的很有用...所以不要只是 $this->upload->do_upload(); 尝试类似的东西

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

            $this->load->view('upload_error', $error);
        }

然后在您的视图中添加一个名为 upload_error.php 的文件,其中包含以下代码:

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>



</body>
</html>

祝你好运!

【讨论】:

    【解决方案3】:

    在像这样设置另一个配置之前尝试取消设置配置。 以下代码取消设置配置并清除图片库。

    你需要清除你的库。

    //Unset config for the next one
      unset($config);
      $this->image_lib->clear();
    

    来自 CI 手册...

    $this->image_lib->clear()

    clear 函数会重置处理图像时使用的所有值。如果您在循环中处理图像,您将需要调用它。

    【讨论】:

      【解决方案4】:

      嗯,这里有两个建议:

      1. 确保您的company 文件夹是可写的
      2. $this-&gt;id 返回什么?如果它返回带有扩展名的文件名,那么这就是导致您的上传过程失败的原因,因为您将拥有一个文件名my_image.png_company,这是不允许的类型。
        请参阅documentation page 了解更多信息。

      【讨论】:

      • $this-&gt;id 只是用户的 ID。上传类在文件命名后自动添加扩展名。
      • 你确定吗?您正在使用哪个版本的 CI?
      【解决方案5】:
       $config = array(
          'upload_path'   './assets/upload/profilepic/',
          'allowed_types' => 'JPG|JPEG|PNG',
          'overwrite' => TRUE,
          'file_name'  => date('YmdHis').''.rand(0,9999)
      ); 
      

      它不适用于 JPEG 文件类型 我也写过小后者,但它没有用。 如果你有东西请分享 JPEG文件不上传其他文件插入方便。

      【讨论】:

        猜你喜欢
        • 2012-02-21
        • 1970-01-01
        • 2013-08-23
        • 2011-06-08
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多