检测空文件上传数组

时间:2014-01-27 02:30:52

标签: php if-statement file-upload foreach

当使用下面的代码来“更新”数据库中的现有记录时,我已经尝试了几种方法(至少6种)来调整这个脚本,以便它只应该将存储的路径写入图像,如果新图像是选择上传。我尝试的每个方法都通过语法检查,但无论如何都得到相同的结果,每次脚本运行时,所选数据库记录中的所有5个“图像”行都被写为BLANK,无论我选择了一个,多个还是没有提交表单中的文件。这个for / script组合在创建新记录时完美无缺,但在使用它来更新现有记录时证明要困难得多。我该怎么写这个,以便只有文件上传字段与新文件一起写入数据库,写入相应的行。

表格更新图像(

<div style="width:98%">Product Image 1:<input type="file" name="prodi[]" />
<?php if (!$row['image1']=="") {echo "<a href='#afrow2' onclick=\"window.open('$row[image1]','_blank','toolbar=yes, scrollbars=yes, resizable=yes, top=300, left=300, width=400, height=400'); \"> Current Image</a>";} ?>
</div>
<div style="width:98%">Product Image 2:<input type="file" name="prodi[]" />
<?php if (!$row['image2']=="") {echo "<a href='#afrow2' onclick=\"window.open('$row[image2]','_blank','toolbar=yes, scrollbars=yes, resizable=yes, top=300, left=300, width=400, height=400'); \"> Current Image</a>";} ?>
</div>
<div style="width:98%">Product Image 3:<input type="file" name="prodi[]" />
<?php if (!$row['image3']=="") {echo "<a href='#afrow2' onclick=\"window.open('$row[image3]','_blank','toolbar=yes, scrollbars=yes, resizable=yes, top=300, left=300, width=400, height=400'); \"> Current Image</a>";} ?>
</div>
<div style="width:98%">Product Image 4:<input type="file" name="prodi[]" />
<?php if (!$row['image4']=="") {echo "<a href='#afrow2' onclick=\"window.open('$row[image4]','_blank','toolbar=yes, scrollbars=yes, resizable=yes, top=300, left=300, width=400, height=400'); \"> Current Image</a>";} ?>
</div>
<div style="width:98%">Product Image 5:<input type="file" name="prodi[]" />
<?php if (!$row['image5']=="") {echo "<a href='#afrow2' onclick=\"window.open('$row[image5]','_blank','toolbar=yes, scrollbars=yes, resizable=yes, top=300, left=300, width=400, height=400'); \"> Current Image</a>";} ?>
</div>

IMAGE UPLOAD / HANDLER SCRIPT

$inum=0;
$image1="";
$image2="";
$image3="";
$image4="";
$image5="";

foreach ($_FILES["prodi"]["error"] as $key => $error)
{
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["prodi"]["tmp_name"][$key];
        $name = $_FILES["prodi"]["name"][$key];
        $type = $_FILES["prodi"] ["type"] [$key];
        if ($type=="image/jpg" OR $type=="image/jpeg" OR $type=="image/pjpeg" OR $type=="image/png")
        {move_uploaded_file($tmp_name, "../images/item-images/$name");$ipath=$domain."/images/item-images/".$name;
        $inum++;
        $i="image".$inum;
        $$i=$ipath;
    }
                                 }
}
if (!$image1=""){mysqli_query($con,"UPDATE rdata SET image1='$image1' WHERE sku=$_POST[itmSku]");}
if (!$image2=""){mysqli_query($con,"UPDATE rdata SET image2='$image2' WHERE sku=$_POST[itmSku]");}
if (!$image3=""){mysqli_query($con,"UPDATE rdata SET image3='$image3' WHERE sku=$_POST[itmSku]");}
if (!$image4=""){mysqli_query($con,"UPDATE rdata SET image4='$image4' WHERE sku=$_POST[itmSku]");}
if (!$image5=""){mysqli_query($con,"UPDATE rdata SET image5='$image5' WHERE sku=$_POST[itmSku]");}

0 个答案:

没有答案