在Codeiniter中调整toupload文件夹的多个图像

时间:2016-02-06 18:21:06

标签: php codeigniter codeigniter-3

我有这个控制器,可以将图像插入上传文件夹而无需调整大小。

public Function Upload() {
    $this->load->library('upload');
    $config['upload_path'] = FCPATH . 'uploads/property-images';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name']      = 'property_image_1';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;
        for ($i = 0; $i < $number_of_files; $i++) {
          $_FILES['uploadedimage']['name'] = $files['name'][$i];
          $_FILES['uploadedimage']['type'] = $files['type'][$i];
          $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
          $_FILES['uploadedimage']['error'] = $files['error'][$i];
          $_FILES['uploadedimage']['size'] = $files['size'][$i];

    $this->upload->initialize($config);
    if ($this->upload->do_upload('uploadedimage', $i)) {

$data['uploadedimage'] = $this->upload->data();
$image_name[$i] = $data['uploadedimage']['file_name'];
//$this->_property_images_resize($data); // Private Function For Resize
$data['images'] = implode(',',$image_name);

$this->model_users->insert_property_details($data))
redirect('view');
  } else {
$this->form_validation->set_message('fileupload_check', $this->upload->display_errors());
return FALSE;
  }
}
}

我还需要在控制器中使用私有函数来调整图像大小:

private function _property_images_resize($data) {
    $this->load->library('image_lib');
    $config = array(
    'image_library'     => 'gd2',
    'source_image'      => 'uploads/property-images/'.$data['uploadedimage']['file_name'],
    'new_image'         => 'uploads/profile/test1/',
    'create_thumb'      => TRUE,
    'maintain_ratio'    => TRUE,
    'width'             => 10,
    'height'            => 10
    );
    $this->image_lib->initialize($config);
    if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
}

添加第二个功能后,它什么都没改变。图像上传之前的方式而不需要调整大小。

寻找别人的帮助,但无法弄清楚。

用于验证回调

function if_image_was_selected(){
$this->form_validation->set_message('if_image_was_selected', 'Please select at least 1 image in jpg/jpeg/png/gif format.');
$number_of_files = count($_FILES['uploadedimages']['tmp_name']);
    $files = $_FILES['uploadedimages'];
    for($i=0;$i<$number_of_files;$i++) {
      if($_FILES['uploadedimages']['error'][$i] != 0) {
    return false;
    }else{
        return true;
    }
    }
}

1 个答案:

答案 0 :(得分:1)

你确定不要打电话:

$title=$_POST['blog_title'];
$result = str_replace(" ", "-", $title);
$description=$_POST['blog_description'];
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$name=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if($error > 0){
    die("error while uploading");
}else{
    $permissible_extension = array("png", "jpg", "jpeg", "svg", "jpg","jpe");
    if(in_array($ext, $permissible_extension)){
        if(move_uploaded_file($temp,"upload/".$name)){
            $sql = mysql_query("INSERT INTO blogs(image,blog_title,blog_description)values('$name','$result','$description')");
            if($sql){
                header("Location:blogimage.php");  
                exit();
            }else{
                echo "Insertion failed";
            }
        }else{
            echo "File couldn't be uploaded";
        }
    }else{
        echo "Invalid format";
    }
}
}else{
$sql = mysql_query("INSERT INTO blogs(blog_title,blog_description)values('$result','$description')");
if($sql){
    header("Location:blogimage.php");  
    exit();
}else{
    echo "Insertion failed";
}
}

而不是

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

您的网络服务器用户是否具有 uploads / profile / test1 / 目录的写入权限?

您收到任何错误消息吗?你检查过PHP日志或Apache日志了吗?

尝试在代码上方添加以下代码,以便在可以的情况下查看错误回显到屏幕。

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