gpt4 book ai didi

PHP - 上传多张图片

转载 作者:太空宇宙 更新时间:2023-11-04 15:37:02 25 4
gpt4 key购买 nike

我需要通过表单上传多张图片。我以为我会毫无问题地做到这一点,但我有一个。

当我尝试执行 foreach 并逐张获取图像时,它并没有像我希望的那样表现。

HTML

<form method="post" action="" enctype="multipart/form-data" id="frmImgUpload">
<input name="fileImage[]" type="file" multiple="true" />
<br />
<input name="btnSubmit" type="submit" value="Upload" />
</form>

PHP

<?php
if ($_POST)
{
echo "<pre>";
foreach ($_FILES['fileImage'] as $file)
{
print_r($file);
die(); // I want it to print first image content and then die to test this out...
//imgUpload($file) - I already have working function that uploads one image
}
}

我期望它打印出第一张图片,但它打印了所有图片的名称。

例子

Array
(
[0] => 002.jpg
[1] => 003.jpg
[2] => 004.jpg
[3] => 005.jpg
)

我想要它输出什么

Array
(
[name] => 002.jpg
[type] => image/jpeg
[tmp_name] => php68A5.tmp
[error] => 0
[size] => 359227
)

那么我如何才能在循环中逐张选择图像以便我可以将它们全部上传?

好吧,我找到了解决方案,我就是这样做的,可能不是最好的方法,但它确实有效。

foreach ($_FILES['fileImage']['name'] as $f)
{
$file['name'] = $_FILES['fileImage']['name'][$i];
$file['type'] = $_FILES['fileImage']['type'][$i];
$file['tmp_name'] = $_FILES['fileImage']['tmp_name'][$i];
$file['error'] = $_FILES['fileImage']['error'][$i];
$file['size'] = $_FILES['fileImage']['size'][$i];
imgUpload($file);
$i++;
}

最佳答案

该数组以另一种方式形成

是这样的:

array ( 
'name' => array (
[0] => 'yourimagename',
[1] => 'yourimagename2',
....
),
'tmp_file' => array (
....

应该这样做:

foreach ($_FILES['fileImage']['name'] as $file)
{
print_r($file);
die(); // I want it to print first image content and then die to test this out...
//imgUpload($file) - I already have working function that uploads one image
}

关于PHP - 上传多张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com