开发者

Multiple file upload with codeigniter

Once again I need some help开发者_StackOverflow中文版.

I'm creating a multiple file upload function with codeigniter and I'm stuck with the "multiple" part.

The follwing code works perfect to upload a single image, it basically uploads an regular size image to a folder and a thumbnail size version of the same image to another folder.

2 images for 2 folders, see my code:

$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();

What I need help with is the loop that that should run this code as many times as images are being uploaded.

I tried a "foreach" loop using something like this:

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

And it gives me this error: Message: Array to string conversion

I've also tried a "for" loop in which I iterate through the code and it kind of works, but it uploads the last image as many times as images were being uploaded.

Hope somebody can share some knowledge with me.

Thanks


I've also tried a "for" loop in which I iterate through the code and it kind of works, but it uploads the last image as many times as images were being uploaded.

It looks like you attempted to fix the problem with a for loop which was only broken because you have the incorrect naming convention for the HTML form.

Try something like the following:

    <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>

Then in your code you can change

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

to

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

You should be able to enclose it in a loop to go through each image you want uploaded with the afor mentioned for loop you were using

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜