访问使用 Plupload 上传的文件时遇到问题

发布于 2024-10-25 13:12:33 字数 1059 浏览 2 评论 0原文

我们正在使用多文件上传器插件,我们希望使用 move_uploaded_file() 将其保存到服务器,并在成功上传后发送包含所有文件直接链接的邮件。我们正在获取以下数组...

Array
(
    [html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
    [html5_uploader_0_name] => show_041.jpg
    [html5_uploader_0_status] => done
    [html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
    [html5_uploader_1_name] => 23.jpg
    [html5_uploader_1_status] => done
    [html5_uploader_count] => 2
)

并且我们正在使用以下代码...

    $("#html5_uploader").pluploadQueue({
    // General settings
    runtimes : 'html5',
    url : 'upload.php',
    max_file_size : '10mb',
    chunk_size : '1mb',
    unique_names : true,
    filters : [
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"}
    ],

    // Resize images on clientside if we can
    //resize : {width : 320, height : 240, quality : 90}
});

}

所以问题是我们应该在 $_FILES["file"]["tmp_name"] 和 $_FILES["file"][ 处传递什么如果是这个数组,则为“name”]。

We are using a multi file uploader plugin and we want to save it to the server using move_uploaded_file() and send mail with direct link of all files after successful upload. We are getting following array...

Array
(
    [html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
    [html5_uploader_0_name] => show_041.jpg
    [html5_uploader_0_status] => done
    [html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
    [html5_uploader_1_name] => 23.jpg
    [html5_uploader_1_status] => done
    [html5_uploader_count] => 2
)

And we are using following code...

    $("#html5_uploader").pluploadQueue({
    // General settings
    runtimes : 'html5',
    url : 'upload.php',
    max_file_size : '10mb',
    chunk_size : '1mb',
    unique_names : true,
    filters : [
        {title : "Image files", extensions : "jpg,gif,png"},
        {title : "Zip files", extensions : "zip"}
    ],

    // Resize images on clientside if we can
    //resize : {width : 320, height : 240, quality : 90}
});

}

so the problem is what should we pass at place of $_FILES["file"]["tmp_name"] and $_FILES["file"]["name"] in case of this array.

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

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

发布评论

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

评论(2

忆悲凉 2024-11-01 13:12:33

感谢您更新帖子。

当单个表单中存在共享数组格式元素名称的多个文件上传时,$_FILES 会变得很奇怪。 它在文件信息之后添加了另一层

想象一下可能会更好:

array(
    'name' => array(
        0 => 'foo.gif',
        1 => 'bar.jpeg'
    ),
    'tmp_name' => array(
        0 => '/tmp/something',
        1 => '/tmp/else',
    )
);

不是你所期望的,对吧?

现在,您的代码正在做一些完全无意义的事情。您正在循环 $_POST,但随后正在处理 $_FILES。事情不是这样的。

我不知道您在哪里获取发布的原始数组,所以我将忽略它。

假设'file'是多文件上传表单元素的名称,完全重建$_FILES数组:(

$fixed_files = array();
foreach($_FILES['file'] as $expected => $unexpected) {
    foreach($unexpected as $index => $data)
        $fixed_files[$index][$expected] = $data;
}

此代码<如果内部值不是数组,则可能会失败 - 请参阅 PHP 手册中的多个上传页面,用于修复 $_FILES 的其他选项。)

那里。现在我们可以像从一开始就应该那样循环遍历 $fixed_files

foreach($fixed_files as $uploaded) {
    $safe_name = preg_replace('/[^A-Za-z0-9_-\.]/', '', $uploaded["name"]);
    move_uploaded_file($uploaded["tmp_name"], "upload/" . $safe_name);
    $links[] = "upload/" . $safe_name;
}

注意之前代码中的新添加内容 - $safe_name。数组的名称元素来自用户,不应被信任。正则表达式会删除除字母、数字、破折号、下划线和句点之外的任何内容。

如果可以的话,您还应该对上传的数据进行更多验证!例如,如果您需要图像,则应该尝试验证文件是否确实是图像。如果您使用的是 PHP 5.3,请查看 finfo_file。您可以使用它来检索文件的 MIME 类型,并确保它是您可以使用的文件类型。

Thanks for the updated post.

When there are multiple file uploads in a single form that share an array-formatted element name, $_FILES gets all wonky. Instead of adding another layer between the form element name and the file information, it adds another layer after the file information.

It might be better to visualize it:

array(
    'name' => array(
        0 => 'foo.gif',
        1 => 'bar.jpeg'
    ),
    'tmp_name' => array(
        0 => '/tmp/something',
        1 => '/tmp/else',
    )
);

Not what you expected, right?

Now, your code is doing something utterly nonsensical. You're looping over $_POST, but then you're processing $_FILES. Things don't work that way.

I have no idea where you're getting the original array you posted, so I'm going to ignore it.

Let's pretend that 'file' is the name of the multi-file upload form element and totally rebuild the $_FILES array:

$fixed_files = array();
foreach($_FILES['file'] as $expected => $unexpected) {
    foreach($unexpected as $index => $data)
        $fixed_files[$index][$expected] = $data;
}

(This code can fail if the inner value is not an array -- see the multiple upload page in the PHP manual for other options to fix $_FILES.)

There. Now we can loop through $fixed_files like we should have been able to from the start:

foreach($fixed_files as $uploaded) {
    $safe_name = preg_replace('/[^A-Za-z0-9_-\.]/', '', $uploaded["name"]);
    move_uploaded_file($uploaded["tmp_name"], "upload/" . $safe_name);
    $links[] = "upload/" . $safe_name;
}

Note a new addition to your previous code -- $safe_name. The name element of the array comes from the user and should not be trusted. The regex strips out anything other than letters, numbers, dashes, underscores and periods.

You should also be doing more verification of the uploaded data, if you can! For example, if you are expecting images, you should attempt to verify that the files are indeed images. If you're using PHP 5.3, check out finfo_file. You can use it to retrieve the file's MIME type, and make sure it's a file type you can work with.

假情假意假温柔 2024-11-01 13:12:33

发布后收到的输出存储在 $_POST 中,这是您显示的数组的来源。

为了更好地利用 $_POST 数组输出,您首先需要将 Form Posts 输出更改为多维数组/输出。

我这样做的方法是将 plupload .js 文件将 html 表单构建为多部分表单的位置。使用以下PHP方法PHP手册中的多上传页面 正如@Charles 提到的。

下面是我编辑的行的示例,使用“[]”括号将计数和“name”标记中的值名称括起来,将 $_POST 数据拆分为多维数组。

    if (file.target_name) {
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][tmpname]" value="' + plupload.xmlEncode(file.target_name) + '" />';
    }
    
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][name]" value="' + plupload.xmlEncode(file.name) + '" />';
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][status]" value="' + (file.status == plupload.DONE ? 'done' : 'failed') + '" />';

变成这样

Array
(
    [html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
    [html5_uploader_0_name] => show_041.jpg
    [html5_uploader_0_status] => done
    [html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
    [html5_uploader_1_name] => 23.jpg
    [html5_uploader_1_status] => done
    [html5_uploader_count] => 2
)

array
  'uploader' => 
    array
      0 => 
        array
          'tmpname' => string 'p15rkfr6oogdt1ln91t0r4obrrh3.jpg'
          'name' => string 'show_041.jpg'
          'status' => string 'done'
      'count' => string '1'

然后您可以使用 Plupload 保存“Upload.php”中找到的临时(tmp)文件的位置以及 tmpname $_POST['tmpname'] 来移动文件。

    $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
    move_uploaded_file($targetDir . $_POST['tmpname'], $destination);

The output you receive after posting is stored in $_POST which is where the array you are displaying has come from.

In order to make better use of the $_POST Array output, you will firstly need to change the Form Posts output into a multidimensional Array/Output.

The way i did this was by changing where the plupload .js file constructs the html form into a multipart form. Using the following PHP method multiple upload page in the PHP manual as @Charles mentioned.

An example of the lines I edited below using the '[]' brackets around the count and the name of the value in the "name" tags which splits the $_POST data up into a multidimensional array.

    if (file.target_name) {
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][tmpname]" value="' + plupload.xmlEncode(file.target_name) + '" />';
    }
    
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][name]" value="' + plupload.xmlEncode(file.name) + '" />';
    inputHTML += '<input type="hidden" name="' + id + '[' + inputCount + '][status]" value="' + (file.status == plupload.DONE ? 'done' : 'failed') + '" />';

Which turns this:

Array
(
    [html5_uploader_0_tmpname] => p15rkfr6oogdt1ln91t0r4obrrh3.jpg
    [html5_uploader_0_name] => show_041.jpg
    [html5_uploader_0_status] => done
    [html5_uploader_1_tmpname] => p15rkfr8mb1plfu4q1giu3v01jl74.jpg
    [html5_uploader_1_name] => 23.jpg
    [html5_uploader_1_status] => done
    [html5_uploader_count] => 2
)

Into this:

array
  'uploader' => 
    array
      0 => 
        array
          'tmpname' => string 'p15rkfr6oogdt1ln91t0r4obrrh3.jpg'
          'name' => string 'show_041.jpg'
          'status' => string 'done'
      'count' => string '1'

Then you can use location of where Plupload saves the temporary (tmp) files found in 'Upload.php' along with the tmpname $_POST['tmpname'] to move the file.

    $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
    move_uploaded_file($targetDir . $_POST['tmpname'], $destination);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文