PHP - 上传多张图片 [英] PHP - Upload multiple images
问题描述
我需要通过表单上传多张图片。我认为我会做没有问题的,但我有一个。
当我尝试做foreach并逐个获取图像时,它并不像我希望的那样行事
HTML
< form method =post action =enctype =multipart / form-dataid =frmImgUpload>
< input name =fileImage []type =filemultiple =true/>
< br />
< input name =btnSubmittype =submitvalue =Upload/>
< / form>
PHP
<?php
if($ _POST)
{
echo< pre>;
foreach($ _FILES ['fileImage'] as $ file)
{
print_r($ file);
die(); //我想要它打印第一个图像内容,然后死去测试...
// imgUpload($ file) - 我已经有了可以上传一张图片的工作函数
}
}
我期望从中打印出第一张图像,而不是打印所有图像的名称。
示例
数组
(
[0] => 002.jpg
[1] => 003.jpg
[2] => 004.jpg
[3] => 005.jpg
$ / code>
我要输出的内容
数组
$ p $因此,我可以在循环中选择图像,以便我可以将它们全部上传?
(
[name] =&>; 002.jpg
[type] => image / jpeg
[tmp_name ] => php68A5.tmp
[error] => 0
[size] => 359227
)
Okey我找到了解决方案这是我如何做的,可能不是最好的方式,但它的工作原理。
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 ++;
}
解决方案重建
$ _ FILES
数组以访问它们的子项目作为一个数组。$ index = 0;
$ field ='fileImage';
$ keys = array_keys($ _ FILES [$ field]);
$ file = array();
foreach($ key为$ key)
{
$ file [$ key] = $ _FILES [$ field] [$ key] [$ index];
}
print_r($ file);
将
$ index
更改为您需要的值选择一个特定的文件。I need to upload multiple images via form. I thought that I will do it with no problem, but I have one.
When I try to do foreach and get image by image it is not acting like I hoped it will.
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 } }
What I expected from it to print out first image, instead it prints names of all the images.
Example
Array ( [0] => 002.jpg [1] => 003.jpg [2] => 004.jpg [3] => 005.jpg )
What I want it to output
Array ( [name] => 002.jpg [type] => image/jpeg [tmp_name] => php68A5.tmp [error] => 0 [size] => 359227 )
So how can I select image by image in the loop so I can upload them all?
Okey I found solution and this is how I did it, probably not the best way but it works.
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++; }
解决方案You are basically asking of how to rebuild the
$_FILES
array to access subitems of them as one array.$index = 0; $field = 'fileImage'; $keys = array_keys($_FILES[$field]); $file = array(); foreach($keys as $key) { $file[$key] = $_FILES[$field][$key][$index]; } print_r($file);
change
$index
to the value you need to pick a specific file.这篇关于PHP - 上传多张图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!