codeigniter php 中的多个图片上传问题

问题描述 投票:0回答:1

我正在尝试使用codeigniter上传多个图像,我有大约15个图像字段,名称为pimage到pimage15,所有都是可选的,我的控制器代码如下:

 public function editprojectplans() {
    $data = array();
    if ($this->isUserLoggedIn) {
        $con = array(
            'id' => $this->session->userdata('userId')
        );
        $data['user'] = $this->user->getRows($con);

        $id = $this->uri->segment(3);
        $data['vals'] = $this->category->selectproject($id);
        if (isset($_POST['editproduct'])) {
            // Check whether user uploaded pictures
            $uploadPath = './uploads/products/';
            $width = 1000; // Define width as per your requirement
            $height = 1000; // Define height as per your requirement

            for ($i = 1; $i <= 15; $i++) {
                $fileInputName = 'pimage'.$i;
                if (!empty($_FILES[$fileInputName]['name'])) {
                    $uploadImgData[$i] = $this->processImageUpload($fileInputName, $uploadPath, $width, $height);
                }
            }
...........................



private function processImageUpload($field, $uploadPath, $width, $height) {
    $this->load->library('upload');
    $this->load->library('image_lib');
    $imageData = array();

    $ImageCount = count($_FILES[$field]['name']);

    for ($i = 0; $i < $ImageCount; $i++) {
        $_FILES['file']['name'] = $_FILES[$field]['name'][$i];
        $_FILES['file']['type'] = $_FILES[$field]['type'][$i];
        $_FILES['file']['tmp_name'] = $_FILES[$field]['tmp_name'][$i];
        $_FILES['file']['error'] = $_FILES[$field]['error'][$i];
        $_FILES['file']['size'] = $_FILES[$field]['size'][$i];

        // File upload configuration
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'jpg|jpeg|png|gif';

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

        // Upload file to server
        if ($this->upload->do_upload('file')) {
            // Uploaded file data
            $imageData[] = $this->upload->data();

            // Resize uploaded image
            $path = $imageData[$i]['full_path'];
            $config['image_library'] = 'gd2';
            $config['source_image'] = $path;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = $width;
            $config['height'] = $height;

            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        } else {
            // Handle upload error
            $error = array('error' => $this->upload->display_errors());
            // You can log or handle the error as per your application's requirement
        }
    }

    return $imageData;
}

我的html是这样的:

<form action="" method="post" enctype="multipart/form-data" id="myform">
  <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
    name="pimage[]">
  <input type="hidden" name="pimage1p" value="<?=$val->floorplan1?>" />

  <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
    name="pimage2[]">
  <input type="hidden" name="pimage2p" value="<?=$val->floorplan2?>" />

  <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
    name="pimage3[]">
  <input type="hidden" name="pimage3p" value="<?=$val->floorplan3?>" /> ...............

  <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
    name="pimage15[]">
  <input type="hidden" name="pimage15p" value="<?=$val->floorplan15?>" />

这里我没有收到任何错误,文件也没有上传,服务器的权限很好,限制一切都很好,但是这里有一些问题,提前感谢

php file codeigniter upload codeigniter-3
1个回答
0
投票
    Step1
    ------------------------------------------
    change html like this
    
    <form action="" method="post" enctype="multipart/form-data" id="myform">
      <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
        name="pimage[]">
      <input type="hidden" name="pimage1p" value="<?=$val->floorplan1?>" />
    
      <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
        name="pimage[]">
      <input type="hidden" name="pimage2p" value="<?=$val->floorplan2?>" />
    
      <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
        name="pimage[]">
      <input type="hidden" name="pimage3p" value="<?=$val->floorplan3?>" /> ...............
    
      <input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
        name="pimage[]">
      <input type="hidden" name="pimage15p" value="<?=$val->floorplan15?>" />
step2
-----------
paste code in controller
// Controller: Upload.php
class Upload extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function index() {
        $this->load->view('upload_form', array('error' => ''));
    }

    public function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 10000; // Adjust as per your need

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

        $upload_data = array();

        // Loop through each file input
        for ($i = 1; $i <= 15; $i++) {
            if (!empty($_FILES['pimage']['name'][$i - 1])) {
                $_FILES['userfile']['name']     = $_FILES['pimage']['name'][$i - 1];
                $_FILES['userfile']['type']     = $_FILES['pimage']['type'][$i - 1];
                $_FILES['userfile']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i - 1];
                $_FILES['userfile']['error']    = $_FILES['pimage']['error'][$i - 1];
                $_FILES['userfile']['size']     = $_FILES['pimage']['size'][$i - 1];

                if ($this->upload->do_upload('userfile')) {
                    $upload_data[$i] = $this->upload->data();
                } else {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('upload_form', $error);
                    return;
                }
            }
        }

        // Handle uploaded data (e.g., save to database)
        // Example: You can access uploaded file paths like $upload_data[1]['file_name'], $upload_data[2]['file_name'], etc.

        $this->load->view('upload_success', array('upload_data' => $upload_data));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.