这个脚本工作得很好..但我只是想知道如果没有选择图像值“无”而不是随机词,我如何将值插入数据库?喜欢 15199660151398490708
<?php
$name = ''; $type = ''; $size = ''; $error = '';
$upload = "asssets/img/";
$new = time().rand();
$target = $upload . $new . basename( $_FILES['photo']['name']);
require_once('includes/config.php');
// If form submitted, insert values into the database.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
$pic=$new .($_FILES['photo']['name']);
$query = "INSERT INTO posting(photo) VALUES('$pic')";
$result = mysql_query($query);
//Writes the photo to the server
if (($_FILES["photo"]["type"] == "image/gif") ||
($_FILES["photo"]["type"] == "image/jpeg") ||
($_FILES["photo"]["type"] == "image/png") ||
($_FILES["photo"]["type"] == "image/pjpeg")) {
$filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
{
echo "";
}
}else
echo "";{
?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">
<input name="photo" type="file" accept="image/*">
<input name="submit" class="button_post" id="click" type="submit" value=" Post" />
</form><?php } ?>
答案 0 :(得分:3)
检查文件输入是否为空它首先是否为空或者返回错误代码,因为如果出现错误它可能为0。
if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
{
$pic = "none";
}
else
{
$pic = $new .($_FILES['photo']['name']);
}
代码将检查文件输入是否为空。如果为空,则会将$pic
变量值设置为&#34; none&#34;否则会生成随机数。
整个代码将是:
<?php
$name = ''; $type = ''; $size = ''; $error = '';
$upload = "asssets/img/";
$new = time().rand();
$target = $upload . $new . basename( $_FILES['photo']['name']);
require_once('includes/config.php');
// If form submitted, insert values into the database.
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
if ($_FILES['photo']['size'] == 0 && $_FILES['photo']['error'] == 0)
{
$pic = "none";
}
else
{
$pic = $new .($_FILES['photo']['name']);
}
$query = "INSERT INTO posting(photo) VALUES('$pic')";
$result = mysql_query($query);
//Writes the photo to the server
if (($_FILES["photo"]["type"] == "image/gif") ||
($_FILES["photo"]["type"] == "image/jpeg") ||
($_FILES["photo"]["type"] == "image/png") ||
($_FILES["photo"]["type"] == "image/pjpeg")) {
$filename = compress_image($_FILES["photo"]["tmp_name"], $target, 80);
{
echo "";
}
}else
echo "";{
?><form method="post" style="padding:0px;margin:0px;display: block;" enctype="multipart/form-data" autocomplete="off">
<input name="photo" type="file" accept="image/*">
<input name="submit" class="button_post" id="click" type="submit" value=" Post" />
</form><?php } ?>