我有一个multipart / form-data表单,但我不要求用户向我发送图片。我正在将文件的名称更改为随机数,并将路径添加到MySQL。每当有人不放置文件时,在我的数据库中就会创建一个没有扩展的新记录器(显而易见)。如何阻止这种情况发生? 抱歉我的英文。
以下是代码中的一部分:
case 'Sleep Aid': {
?>
<section class="summarySection">
<div class="summaryHeadingDIV">
<h1 class="summaryH1">Please check the details below</h1>
</div>
<article class="summaryPage">
<p class="boldSummaryDescr">Brand:</p><p class="paraSummary"><?php echo $brand; ?></p><br>
<p class="boldSummaryDescr">Model:</p><p class="paraSummary"><?php echo $model; ?></p><br>
<p class="boldSummaryDescr">Colour:</p><p class="paraSummary"><?php echo $colour; ?></p><br>
<p class="boldSummaryDescr">Material:</p><p class="paraSummary"><?php echo $material; ?></p><br>
<p class="boldSummaryDescr">Suitable Age:</p><p class="paraSummary"><?php echo $suitable; ?></p><br>
<p class="boldSummaryDescr">Purchase date:</p><p class="paraSummary"><?php echo $month . '/' . $year; ?></p><br>
<p class="boldSummaryDescr">Condition:</p><p class="paraSummary"><?php echo $condition; ?></p><br>
<p class="boldSummaryDescr">Dimesions:</p><p class="paraSummary"><?php echo $width . ' cm <span class="smallcaps">x</span> ' . $height . ' cm <span class="smallcaps">x</span> ' . $depth . ' cm'; ?></p><br>
<p class="boldSummaryDescr">Weight:</p><p class="paraSummary"><?php echo $weight . ' kg'; ?></p>
</article>
<div class="imageg_container">
<figure class="img_figure_show">
<?php
if(empty($picture))
{
?>
<img src="/_images/no-picture.png" alt="no picture added">
<?php
}
else
{
$random_id = rand_img_id();
add_file(db_user_connect(), $email, $random_id);
$arry = show_picture(db_user_connect(), $email, $random_id);
print "<img src=".$arry["sciezka"]." alt='product picture'>";
}
?>
</figure>
</div>
<div class="clear-left"></div>
<div class="summaryButtonContainer">
<a href="javascript:goBack();" class="summaryLinks" title="Go Back">Edit details</a>
<a href="/_pages/transaction/quotation.php?location=sleepaid&brand=<?php echo urlencode($brand);?>&model=<?php echo urlencode($model)?>&month=<?php echo urlencode($month);?>&year=<?php echo urlencode($year);?>&condition=<?php echo urlencode($condition);?>" class="summaryLinks">Proced to Quote</a>
</div>
</section>
<?php
break;
}
答案 0 :(得分:1)
您需要检查$_FILES['error']
值:
if ($_FILES['name']['error'] === UPLOAD_ERR_OK) ...
手册列出了所有可能的值,您可以使用这些值来显示不同的消息/执行不同的操作:https://secure.php.net/manual/en/features.file-upload.errors.php