【问题标题】:Image not resize and upload in Codeigniter PHP图像未在 Codeigniter PHP 中调整大小和上传
【发布时间】:2020-04-12 15:33:32
【问题描述】:

我正在尝试使用 codeigniter 创建上传图像的缩略图(调整大小), 但是图片没有上传到服务器/文件夹,这是我的代码,我哪里出错了?

if (isset($_FILES['image'])) {
    if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {

        $filename         = time() . uniqid(rand()) . $_FILES['image']['name'];
        move_uploaded_file($_FILES["image"]["tmp_name"], "vendorProfile/" . $filename);
        $saveArr['image'] = $filename;
        $this->load->library('image_lib');

        $source_paths = base_url() . 'vendorProfile/' . $filename;
        $source_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/' . $filename;
        $target_path  = $_SERVER['DOCUMENT_ROOT'] . '/Profile/vendorProfile/thumb';

        $config_manip = array(
            'image_library'  => 'gd2',
            'source_image'   => $source_path,
            'new_image'      => $target_path,
            'maintain_ratio' => TRUE,
            'create_thumb'   => TRUE,
            'thumb_marker'   => '_thumb',
            'width'          => 150,
            'height'         => 150
        );

        $this->load->library('image_lib', $config_manip);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        } else {
            echo "image is uploaded";
        }
        $this->image_lib->clear();
    } 
}       

【问题讨论】:

  • 嗨。提供更多信息。您看到了什么错误,$_FILES['image'] 中有什么内容,Web 服务器日志中有什么内容等
  • 表单是什么样子的?是否设置了 $_FILES['image'](在检查阶段)?

标签: php image codeigniter


【解决方案1】:

您只需查看在 $config_manip 数组中传递的文件路径,您需要在没有 Http 或 https 的情况下传递文件路径 PHP 不接受 Http。因此,如果在您的情况下,您的文件结构是这样的

-
application
system
image_folder <----

然后你就这样通过

$config_manip = array(
        'image_library'  => 'gd2',
        'source_image'   => './image_folder/'.$file_name,
        'new_image'      => './thumb_folder/'.$target_path,
        'maintain_ratio' => TRUE,
        'create_thumb'   => TRUE,
        'thumb_marker'   => '_thumb',
        'width'          => 150,
        'height'         => 150
    );

【讨论】:

    【解决方案2】:

    如果您使用的是 ubuntu 服务器,您需要授予该文件夹上传文件/图像的权限。

    if (!file_exists($target_path)) {
        if (!mkdir($target_path, 0777, true)) {
            chmod($target_path, 0777);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 2015-06-19
      • 1970-01-01
      • 2014-03-14
      • 2013-12-07
      • 1970-01-01
      • 2013-09-19
      • 2011-08-05
      • 2011-04-16
      相关资源
      最近更新 更多