ZendFramework - 如何使用 zend_http_client 上传多个文件?

发布于 2025-01-04 20:36:49 字数 1059 浏览 0 评论 0原文

如何使用->setFileUpload()上传多个文件?

例如我有这个:

<form method="post" 
      action="/file/csvupload" 
      target="myadmin_b20301" 
      enctype="multipart/form-data">
  <input type="text" name="test" id="test" value="">
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="submit" name="submit" id="submit" value="Update">              
</form>

PHP:(主要上传后,它推送到管理面板,这是另一篇文章)。

....
if (!$upload->isValid()) // /file/csvupload
{
....
  $client = new Zend_Http_Client(); // upload now to administrator another copy
  $client->setUri($uri);
  $client->setParameterPost(array('test'=>'test');
  //how do i tell here to use those same file[] which are multiple?   
  $client->setFileUpload('/tmp/Backup.tar.gz', 'bufile');       
  $client->request( );

How to upload multiple files with ->setFileUpload()?

For example i have this:

<form method="post" 
      action="/file/csvupload" 
      target="myadmin_b20301" 
      enctype="multipart/form-data">
  <input type="text" name="test" id="test" value="">
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="file" name="file[]" id="attachment" class="attachment">   
  <input type="submit" name="submit" id="submit" value="Update">              
</form>

PHP: (after main upload, it push to admin panel which is another post).

....
if (!$upload->isValid()) // /file/csvupload
{
....
  $client = new Zend_Http_Client(); // upload now to administrator another copy
  $client->setUri($uri);
  $client->setParameterPost(array('test'=>'test');
  //how do i tell here to use those same file[] which are multiple?   
  $client->setFileUpload('/tmp/Backup.tar.gz', 'bufile');       
  $client->request( );

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

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

发布评论

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

评论(2

小姐丶请自重 2025-01-11 20:36:49

对上述解决方案的补充是将 ->setOptions(array('multiple' => 'multiple')) 添加到表单文件元素。或者只是将属性 multiple="multiple" 添加到您的 HTML 表单输入标记中。

Multiple 是一种 HTML5 属性,允许一次选择多个文件,而不是每个文件元素选择一个。

An addition to the solutions above is to add ->setOptions(array('multiple' => 'multiple')) to your form file element. Or just the attribute multiple="multiple" to your HTML form input tag.

Multiple is a HTML5 Attribute and allows multiple file selections at once instead of one for each file element.

最美的太阳 2025-01-11 20:36:49

您只需为要上传的每个文件调用一次 setFileUpload() 即可。每次调用都会将文件添加到将上传的文件数组中。

如果您想将文件数组上传到同一文件元素,请将代码更改为:

$client->setFileUpload('/tmp/Backup.tar.gz',    'bufile[]');
$client->setFileUpload('/tmp/DB.Backup.tar.gz', 'bufile[]');

在文件名上使用数组括号表示法,就像在 HTML 中一样。

这导致:

$_FILES = array(
    'name' => array(
        0 => 'Backup.tar.gz',
        1 => 'DB.Backup.tar.gz',
    ),
    'tmp_name' => array(
        0 => '/tmp/php63832' // Backup.tar.gz
        1 => '/tmp/php33248' // DB.Backup.tar.gz
    ),
    // etc
);

You can just call setFileUpload() once for each file that you want to upload. Each call adds the file to an array of files that will be uploaded.

If you would like to upload an array of files to the same file element, change your code to this:

$client->setFileUpload('/tmp/Backup.tar.gz',    'bufile[]');
$client->setFileUpload('/tmp/DB.Backup.tar.gz', 'bufile[]');

Use the array bracket notation on the filename just like you would in HTML.

This results in:

$_FILES = array(
    'name' => array(
        0 => 'Backup.tar.gz',
        1 => 'DB.Backup.tar.gz',
    ),
    'tmp_name' => array(
        0 => '/tmp/php63832' // Backup.tar.gz
        1 => '/tmp/php33248' // DB.Backup.tar.gz
    ),
    // etc
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文