如何让 Opera 像其他浏览器一样上传文件?

发布于 2024-09-26 11:00:49 字数 1601 浏览 3 评论 0原文

在 Opera 中使用 上传一个文件时,它会按预期工作。也就是说,您可以在 PHP 服务器端的 $_FILES 中找到所需的文件数据。

但是,当我尝试使用 Opera 一次上传多个文件时,通过设置 然后所有文件的内容粘合在一起形成一长串并作为 POST 数据发送。该字符串中的所有文件均由如下标头分隔:据

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1069225496.xml"
Content-Type: text/xml

<?xml>
...

我所知,Opera 遵循 Webforms 2.0 标准。但是有没有一种简单的方法可以让 Opera 以与其他浏览器相同的方式发送多个文件,或者我必须编写一个解释器来仅从 Opera 获取文件?

感谢您的任何帮助。下面是我当前使用的 HTML。


<div id="filearea">
    <input type="file" min="1" max="6000" accept="text/xml" name="file[]" style="padding: 1px; margin: 2px 0px;" />
</div>

这就是 $_POST 的 var_dump 的样子(我已经删除了任何实际的 XML 数据,占用了空间)

array(1) {
  ["file"]=>
  array(1) {
    [0]=>
    string(4209) "------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="1219854274.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1069225496.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1111008062.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="1219854274.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...
    "
  }
}

When uploading one file using <input type="file" /> in Opera, it works as expected. That is, you find the expected file data in $_FILES in PHP server side.

However, when I try to upload several files at once using Opera, by setting <input type="file" min="1" max="999" /> then all the files' contents are glued together in one long string and sent as POST data. All the files in this string are separated by headers such as this:

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1069225496.xml"
Content-Type: text/xml

<?xml>
...

Opera follows the Webforms 2.0 standard, I know. But is there a simple way to make Opera send multiple files in the same fashion other browsers do, or will I have to write an interpreter to get files just from Opera?

Thanks for any help. Below is the HTML I'm currently using.


<div id="filearea">
    <input type="file" min="1" max="6000" accept="text/xml" name="file[]" style="padding: 1px; margin: 2px 0px;" />
</div>

This is how the var_dump of $_POST looks (I've erased any actual XML data, taking up space)

array(1) {
  ["file"]=>
  array(1) {
    [0]=>
    string(4209) "------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="1219854274.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1069225496.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="xxx1111008062.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...

------------94QV8HRqBwta8NY4L2WH0r
Content-Disposition: form-data; name="file[]"; filename="1219854274.xml"
Content-Type: text/xml

<?xml version="1.0"?>
...
    "
  }
}

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

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

发布评论

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

评论(1

萌吟 2024-10-03 11:00:49

我刚刚检查了一份 PHP 错误报告,它声称这在 Opera 中有效:

<input type="file" name="file" min="1" max="999" />

但这不起作用:

<input type="file" name="file[]" min="1" max="999" />

编辑:经过测试,我相信将错误标记为虚假的 PHP 人不知道他在说什么。 ..我无法通过任何一种方式使用 PHP 进行本地工作。

据我所知,PHP 不支持 Opera 的“混合”文件上传。这不是 Opera 的错误,因为他们是按照 RFC 的规范来实现的。我相信其他浏览器只是上传文件,就好像有多个输入元素一样。您可以通过检查 _POST 数组轻松添加对此的支持:

   $file = $_POST['file'][0];

   while (preg_match('/^(-+[A-Za-z0-9]+)\s+/', $file, $matches))
   {
      $id = $matches[1];

      $i = strlen($matches[0]);
      $body = false;
      $headers = array();
      while (($j = strpos($file, "\n", $i)) !== false)
      {
         $line = substr($file, $i, $j - $i);
         $i = $j + 1;
         if (trim($line) == '')
         {
            $body = true;
            break;
         }

         list($key, $val) = explode(':', trim($line), 2);
         $headers[$key] = trim($val);
      }
      if (!$body) break;

      $j = strpos($file, $id, $i);

      $data = substr($file, $i, $j-$i);
      echo $data."<HR>"; // also check $headers

      $file = substr($file, $j);
   }

上面的代码中可能存在一些相差一的错误。

I just checked a PHP bug report, and it claimed that this works in Opera:

<input type="file" name="file" min="1" max="999" />

But that this does not:

<input type="file" name="file[]" min="1" max="999" />

Edit: After testing this, I believe the PHP person who marked the bug as bogus didn't know what he was talking about... I cannot get either way to work natively with PHP.

As far as I can tell, PHP does not support Opera's 'mixed' file uploads. This is not a bug on Opera's part, as they are implementing it per the RFC's specification. I believe the other browsers simply upload the files as if there were multiple input elements. You could easily add support for this by checking the _POST array:

   $file = $_POST['file'][0];

   while (preg_match('/^(-+[A-Za-z0-9]+)\s+/', $file, $matches))
   {
      $id = $matches[1];

      $i = strlen($matches[0]);
      $body = false;
      $headers = array();
      while (($j = strpos($file, "\n", $i)) !== false)
      {
         $line = substr($file, $i, $j - $i);
         $i = $j + 1;
         if (trim($line) == '')
         {
            $body = true;
            break;
         }

         list($key, $val) = explode(':', trim($line), 2);
         $headers[$key] = trim($val);
      }
      if (!$body) break;

      $j = strpos($file, $id, $i);

      $data = substr($file, $i, $j-$i);
      echo $data."<HR>"; // also check $headers

      $file = substr($file, $j);
   }

There may be some off-by one errors in the above code.

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