【问题标题】:Codeigniter upload file and resizeCodeigniter 上传文件并调整大小
【发布时间】:2015-07-30 07:15:27
【问题描述】:

我正在尝试调整预上传图像的大小,然后通过 FTP 将它们发送到另一台服务器,但它似乎不起作用。上传工作正常,ftp 也工作正常,但每当我下载图像并检查大小时,它与上传的文件相同。

这是我的控制器:

if ($this->upload->do_upload())
        {
            $data = $this->upload->data();
            $image = $data['file_name'];

            $this->load->library('image_lib');

            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/devices/'.$image;
            $config['maintain_ratio'] = TRUE;
            $config['width']    = 400;
            $config['height']   = 300;

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

            $localPath = './uploads/devices/'.$image;
            $remotePath = 'webspace/httpdocs/uploads/devices/'.$image;

            $this->load->library('ftp');
            $config['hostname'] = '';
            $config['username'] = '';
            $config['password'] = '';
            $config['port']     = 21;
            $config['passive']  = TRUE;
            $this->ftp->connect($config);
            $this->ftp->upload($localPath, $remotePath);
            $this->ftp->close();
        }

我想要实现的是上传图片,调整大小并替换它,然后上传调整大小的图片。

非常感谢您的帮助!

【问题讨论】:

  • 在配置中设置 log_threshold 为 4,查看日志看看会发生什么
  • @Ara 我已经这样做了,但是我的日志文件夹是空的。我删除了图像和产品并尝试重新添加它们,但日志仍然为空。
  • 您是否将日志文件夹权限设置为 777(所有人可读写)?
  • @Ara 是的,我愿意,但是权限 644 还不够吗?之前有644,现在改成777,还是没有。
  • 666 用于可写文件,777 用于可写文件夹,我的意思是 logs folder 位于 application 文件夹内(不是在谈论文件),如果您确定它是 777 ,请检查您的配置并确保 $config['log_path'] = ''; ('' = 默认) 和 log_threshold = 4

标签: codeigniter upload resize


【解决方案1】:

最终编辑:

使用初始化来传递配置,而不是直接将它们传递给加载->库:

if ($this->upload->do_upload())
    {
        $data = $this->upload->data();
        $image = $data['file_name'];

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/devices/'.$image;
        $config['maintain_ratio'] = TRUE;
        $config['width']    = 400;
        $config['height']   = 300;

        $this->load->library('image_lib');
        $this->image_lib->initialize($config);

        $this->image_lib->resize();

        $localPath = './uploads/devices/'.$image;
        $remotePath = 'webspace/httpdocs/uploads/devices/'.$image;

        $this->load->library('ftp');
        $config['hostname'] = '';
        $config['username'] = '';
        $config['password'] = '';
        $config['port']     = 21;
        $config['passive']  = TRUE;
        $this->ftp->connect($config);
        $this->ftp->upload($localPath, $remotePath);
        $this->ftp->close();
    }

【讨论】:

  • 我的 ftp 执行得很好,所以不能这样。我的上传也是一样,图片正在上传中。
  • 这似乎解决了我的问题,我想问题是我必须添加: $this->image_lib->initialize($config);我不打算在我的构造中加载库,因为里面只有 2 个需要它的函数。
  • 我仍然想知道它是如何修复的,因为据我所知 $this->image_lib->clear();仅在可以具有不同值的循环内使用,并且 $this->image_lib->initialize($config) 应该与 $this->load->library('image_lib', $config); 相同如果我是对的?
  • @killstreet 很高兴听到这个消息,建议将其添加到构造中,看看它是否也无法加载到那里
  • 如果你在构造中不用它就不需要 clear() ,你试过$this->load->library('image_lib'); $this->image_lib->initialize($config); 吗? (用清除改变负载)
猜你喜欢
  • 2013-06-01
  • 1970-01-01
  • 2011-05-18
  • 2015-06-19
  • 2011-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多