我正在构建一个函数,它可以上传单个图像或多个图像,这取决于它是否来自一个字段数组。
下面是函数和调用者:
private function imageUpload($image, $altText, $uploadDir)
{
$uploadDir = (!substr($uploadDir, "/")) ? $uploadDir."/" : $uploadDir;
print_r($_POST[$image]);
if(is_array($_POST[$image]))
{
foreach($_FILES[$image] as $image) //Line 316
{
//$targetPath = $uploadDir.basename($image['name']);
//echo $targetPath;
echo "array";
}
}
else
{
$targetPath = $uploadDir.basename($image['name']);
//echo $targetPath;
echo "not array";
}
}
$this->imageUpload('pei_image', 'pei_alt', '/images/upload/products/');我尝试在pei_image[]字段中使用1/2图像字段,但我得到了以下错误:
遇到PHP错误 严重性:注意消息:未定义索引: pei_image 文件名: models/products_model.php 线路号码: 316 遇到PHP错误 严重程度:警告 消息:为foreach()提供的无效参数 文件名: models/products_model.php 线路号码: 316
但是,当我打印$_POST$image时,我得到了一个数组,它的值数量是正确的。结果如下:
数组( => 06 Lect03 WD概念- mod SP.png 1 => brtemplate.jpg )
怎么啦?
编辑
$_FILES数组是空的,为什么会这样?以下是表单中的两个图像上传字段:
<input type="file" name="pei_image[]" value="Showing-the-Heavy-horse.jpg" class="imageUpload" onchange="removePreviewButton(this, 2);">
<input type="file" name="pei_image[]" value="Showing-the-Heavy-horse.jpg" class="imageUpload" onchange="removePreviewButton(this, 3);">发布于 2012-07-12 16:07:16
这个问题是我的附件类型中的一个错误,因此它默认为“application/x form-urlencoded”,这样它就不会上传到$_FILES数组。
发布于 2012-07-12 12:35:23
替换
foreach($_FILES[$image] as $image)通过
foreach($_FILES as $image)发布于 2012-07-12 12:42:13
试试这个..。
private function imageUpload($image, $altText, $uploadDir)
{
$uploadDir = (!substr($uploadDir, "/")) ? $uploadDir."/" : $uploadDir;
if(is_array($image)
{
foreach($image as $img) //Line 316
{
//$targetPath = $uploadDir.basename($image['name']);
//echo $targetPath;
echo "array";
}
}
else
{
$targetPath = $uploadDir.basename($image['name']);
//echo $targetPath;
echo "not array";
}
}
$this->imageUpload('pei_image', 'pei_alt', '/images/upload/products/');https://stackoverflow.com/questions/11451862
复制相似问题