繁体   English   中英

使用codeigniter上传多个文件

[英]Multiple file upload with codeigniter

我再次需要一些帮助。
我正在使用codeigniter创建一个多文件上传功能,并且停留在“多个”部分。
以下代码非常适合上载单个图像,它基本上是将常规尺寸的图像上载到文件夹,而将同一图像的缩略图尺寸上载到另一个文件夹。
2个文件夹的2张图片,请参见我的代码:

$config['upload_path'] = './cars/large_thumb/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['file_name'] = $newfilename;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$uploadimages = $this->upload->do_upload();
$image_data = $this->upload->data();

$configlarge = array(
    'source_image' => $image_data['full_path'],
    'new_image' => './cars/large_thumb',
    'maintain_ratio' => true,
    'quality' => 70,
    'width' => 600,
    'height' => 450
);
$this->load->library('image_lib', $configlarge);
$this->image_lib->initialize($configlarge);
$resizelarge = $this->image_lib->resize();

$configsmall = array(
    'source_image' => $image_data['full_path'],
    'new_image' => './cars/small_thumb',
    'maintain_ratio' => true,
    'width' => 100,
    'height' => 75
);
$this->load->library('image_lib', $configsmall);
$this->image_lib->initialize($configsmall);
$resizesmall = $this->image_lib->resize();

我需要帮助的是循环,该循环应在图像被上传时运行此代码多次。
我尝试使用以下方式进行“ foreach”循环:

foreach ($_FILES["userfile"]["error"] as $key => $error)  {
    code above here...
  }  

它给了我这个错误:消息:数组到字符串的转换
我还尝试了一个“ for”循环,在该循环中,我遍历了代码并进行了某种工作,但它上载最后一张图像的次数与上载图像的次数相同。

希望有人可以和我分享一些知识。

谢谢

我还尝试了一个“ for”循环,在该循环中,我遍历了代码并进行了某种工作,但它上载最后一张图像的次数与上载图像的次数相同。

似乎您尝试使用for循环解决此问题,该循环只是因为您对HTML表单的命名约定不正确而被打破。

尝试如下操作:

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="car_1" value="" />
    <input type="file" name="car_2" value="" />
    <input type="file" name="car_3" value="" />
    </form>

然后,您可以在代码中进行更改

    $uploadimages = $this->upload->do_upload(); 

    $uploadimages = $this->upload->do_upload('car_1'); 

您应该能够将其围成一个循环,以使用要使用的for循环提到的afor浏览要上传的每个图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM