多次上传未按预期工作

发布于 2024-11-07 17:38:27 字数 1577 浏览 0 评论 0原文

我有一个表单,允许用户添加任意​​数量的上传表单。所以一开始就有一个,您可以单击“添加更多”来...添加更多。有道理。每个输入如下所示:

<input type="file" name="userfile[]" id="userfile[]">

但是,当您上传多个文件时,只会上传第一个文件。下面是我的PHP。我正在使用 CodeIgniter 和文件上传库。

$dir = random_string('alnum', 10);
mkdir('/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir);

$this->load->library('upload');
$error = 0;

for ($i = 0; $i < count($_FILES['userfile']['name']); $i++):
    $_FILES['userfile']['name']                = $_FILES['userfile']['name'][$i];
    $_FILES['userfile']['type']                = $_FILES['userfile']['type'][$i];
    $_FILES['userfile']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
    $_FILES['userfile']['error']            = $_FILES['userfile']['error'][$i];
    $_FILES['userfile']['size']                = $_FILES['userfile']['size'][$i];

    $config['upload_path']                         = '/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir;
    $config['allowed_types']                    = 'jpg|jpeg|gif|png';

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

    if ($this->upload->do_upload('userfile')):
        $error += 0;
    else:
        $error += 1;
    endif;
endfor;

if ($error > 0):
    $error = array('error' => $this->upload->display_errors());
    print_r($error);
else:
    $data = array('upload_data' => $this->upload->data());
    print_r($data);
endif;

如果我在 for 循环之前添加 echo count($_FILES['userfile']['name']; ,它会正确显示文件数。如果我在 for 循环之后使用它,它错误显示 1.

知道我在这里做错了什么吗?

I have a form that allows the user to add as many upload forms as they want. So initially there's one, and you can click "Add More" to... add more. Makes sense. Each input looks like this:

<input type="file" name="userfile[]" id="userfile[]">

But when you upload more than one file, only the first file gets uploaded. Below is my PHP. I'm using CodeIgniter and the File Upload library.

$dir = random_string('alnum', 10);
mkdir('/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir);

$this->load->library('upload');
$error = 0;

for ($i = 0; $i < count($_FILES['userfile']['name']); $i++):
    $_FILES['userfile']['name']                = $_FILES['userfile']['name'][$i];
    $_FILES['userfile']['type']                = $_FILES['userfile']['type'][$i];
    $_FILES['userfile']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
    $_FILES['userfile']['error']            = $_FILES['userfile']['error'][$i];
    $_FILES['userfile']['size']                = $_FILES['userfile']['size'][$i];

    $config['upload_path']                         = '/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir;
    $config['allowed_types']                    = 'jpg|jpeg|gif|png';

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

    if ($this->upload->do_upload('userfile')):
        $error += 0;
    else:
        $error += 1;
    endif;
endfor;

if ($error > 0):
    $error = array('error' => $this->upload->display_errors());
    print_r($error);
else:
    $data = array('upload_data' => $this->upload->data());
    print_r($data);
endif;

If I add an echo count($_FILES['userfile']['name']; before the for loop, it correctly displays the number of files. If I use it after the for loop, it incorrectly displays 1.

Any idea what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆凄美了谁 2024-11-14 17:38:27

我最好的猜测是,一旦进入循环,您将使用以下几行覆盖值:

$_FILES['userfile']['name']                = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type']                = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']            = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size']                = $_FILES['userfile']['size'][$i];

尝试将其更改为如下所示:

$_FILES['userfile_tmp']['name']                = $_FILES['userfile']['name'][$i];
$_FILES['userfile_tmp']['type']                = $_FILES['userfile']['type'][$i];
$_FILES['userfile_tmp']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile_tmp']['error']            = $_FILES['userfile']['error'][$i];
$_FILES['userfile_tmp']['size']                = $_FILES['userfile']['size'][$i];

然后使用 if ($this->upload->do_upload('userfile_tmp')) :

My best guess is that once you enter the loop you overwrite the values using these lines:

$_FILES['userfile']['name']                = $_FILES['userfile']['name'][$i];
$_FILES['userfile']['type']                = $_FILES['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']            = $_FILES['userfile']['error'][$i];
$_FILES['userfile']['size']                = $_FILES['userfile']['size'][$i];

Try changing that to something like this:

$_FILES['userfile_tmp']['name']                = $_FILES['userfile']['name'][$i];
$_FILES['userfile_tmp']['type']                = $_FILES['userfile']['type'][$i];
$_FILES['userfile_tmp']['tmp_name']        = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userfile_tmp']['error']            = $_FILES['userfile']['error'][$i];
$_FILES['userfile_tmp']['size']                = $_FILES['userfile']['size'][$i];

And then using if ($this->upload->do_upload('userfile_tmp')):

天煞孤星 2024-11-14 17:38:27

几个月前我遇到了这个问题,并扩展了核心库以允许单个文件或文件数组(正如您所希望的那样)并适当地显示错误等。

它似乎适合我使用它的用途,不能保证 100% 但看看它是否有帮助,我想我评论了我所做的更改的不同部分

: com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php" rel="nofollow">https://github.com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php

I encountered this problem a few months back and extended the core library to either allow a single file or also a file array (as you're looking to do) and display errors, etc. appropriately.

It seemed to work for what I was using it for, can't guarantee it 100% but see if it helps at all, I think I commented different sections with changes I had made:

https://github.com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php

百变从容 2024-11-14 17:38:27

问题是这些行:

$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];

您正在用另一个值(字符串)覆盖变量(数组)。

The problem are these lines:

$_FILES['userfile']['name'] = $_FILES['userfile']['name'][$i];

You are overwriting your variable, an array, with another value, a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文