我有一个不需要用于图片上传的字段..问题是,如果用户不添加图片,它仍然会添加重命名的图像,而不会显示。
所以基本上它在数据库中放了一个名字
if (isset($_POST['submit'])) {
$Name = $_POST['name'];
$Description = $_POST['description'];
$file = $_FILES['photo']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$rename_image = generate_random(12);
$file_loc = $_FILES['photo']['tmp_name'];
$file_type = $_FILES['photo']['type'];
$file_size = $_FILES['photo']['size'];
$newFilename = $rename_image . '.' . $ext;
$folder = "../uploads/";
if (empty($Name)) {
$errors[] = "Name Required.";
}
if (!empty($_FILES['photo']['name'])) {
}
if (($file_size > 2097152)) {
echo validation_errors("Your avatar exceeds file size");
} elseif (($file_type != "image/jpeg") && ($file_type != "image/jpg") && ($file_type != "image/gif") && ($file_type != "image/png")) {
echo validation_errors("Invalid image format.");
if (!empty($errors)) {
echo validation_errors($errors[0]);
} else {
move_uploaded_file($file_loc, $folder.$newFilename);
$db = dbconnect();
$stmt = $db->prepare("INSERT INTO discussion_categories(Name, Description, Photo) VALUES (?,?,?)");
$stmt->bind_param('sss', $Name, $Description, $newFilename);
$stmt->execute();
$stmt->close();
header("Location: managecategories.php");
}
}
}
答案 0 :(得分:1)
在使用之前检查参数变量是否已设置。
if (windowWidth == sec1LogoWidth) {
$('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))),'left': 0, 'margin-left': 10, 'margin-right': 10 });
} else {
$('#sec1-logo img').css({'top': Math.abs(windowHeight - ((sec1LogoHeight*2)+(Math.ceil(sec1LogoHeight/2)))) - 100,'left': Math.abs((windowWidth / 2) - (sec1LogoWidthImg-(Math.ceil(sec1LogoWidthImg/2)))) });
}
答案 1 :(得分:0)
使用file_exists
或/和is_uploaded_file
函数检查文件是否存在或上传。
if(file_exists($_FILES['photo']['tmp_name']) || is_uploaded_file($_FILES['photo']['tmp_name']) {
$file = $_FILES['photo']['name'];
$ext = pathinfo($file, PATHINFO_EXTENSION);
$rename_image = generate_random(12);
$file_loc = $_FILES['photo']['tmp_name'];
$file_type = $_FILES['photo']['type'];
$file_size = $_FILES['photo']['size'];
$newFilename = $rename_image.
'.'.$ext;
$folder = "../uploads/";
if (($file_size > 2097152)) {
echo validation_errors("Your avatar exceeds file size");
}
else if(($file_type != "image/jpeg") && ($file_type != "image/jpg") && ($file_type != "image/gif") && ($file_type != "image/png")) {
echo validation_errors("Invalid image format.");
}
else {
move_uploaded_file($file_loc, $folder.$newFilename);
}
}