这是我的 postaddata.php 代码:
require('includes/database.php');
$db = new Database();
if(isset($_POST['btnPostad']))
{
$allowedextensions = array("jpg", "jpeg", "gif", "png");
$extensions = end(explode ("." , $_FILES['file']['name']));
if((($_FILES['file']['type'] == 'image/gif')
|| ($_FILES["file"]['type'] == 'image/jpg')
|| ($_FILES["file"]['type'] == 'image/png')
|| ($_FILES["file"]['type'] == 'image/jpeg'))
&& ($_FILES["file"]['size'] > 20000)
&& (in_array($extensions, $allowedextensions)))
{
if($_FILES['file']['error']> 0)
{
$msg = "Error: " . $_FILES['file']['error'] . "<br>";
header('Location:postad.php?msg='.$msg);
}
else
{
$msg2 = "Upload " . $_FILES['file']['name'] . "<br>";
$msg2 .= "Type " . $_FILES['file']['type'] . "<br>";
$msg2 .= "Size " . ($_FILES['file']['size'] / 1024 ). "KB" . "<br>";
$msg2 .= "Stored In " . $_FILES['file']['tmp_name'] . "<br>";
if (file_exists("uploads/". $_FILES['file']['name']))
{
$msg = $_FILES['file']['name']. "already exists";
header('Location:postad.php?msg='.$msg);
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/" . $_FILES['file']['name']);
//$msg1 = "Stored in uploads/: " . $_FILES['file']['name'];
}
}
}
else
{
$msg = '<b> <font face="Verdana, Geneva, sans-serif" color="red">File type is not Supported, Please try uploading recommended formats (JPG, JPEG, GIF, PNG)</font></b>';
header('Location:postad.php?msg='.$msg);
}
//header('Location:postad.php?msg='.$msg1);
$pic = ($_FILES['file']['name']);
$data = array
(
'Ad_Status' => $_POST['adstatus'],
'Model_Year' => $_POST['model_year'],
'Car_Make' => $_POST['car_make'],
'Car_Model' => $_POST['car_model'],
'City' => $_POST['city'],
'Price' => $_POST['price'],
'Mileage' => $_POST['mileage'],
'Mileage_Unit' => $_POST['mileage_unit'],
'Transmission' => $_POST['transmission'],
'Engine_Type' => $_POST['engine_type'],
'Engine_Capacity' => $_POST['engine_capacity'],
'Exterior_Color' => $_POST['exterior_color'],
'Air_Bags' => $_POST['air_bags'],
'Air_Conditioner' => $_POST['air_conditioner'],
'Power_Steering' => $_POST['power_steering'],
'Power_Locks' => $_POST['power_locks'],
'Power_Mirror' => $_POST['power_mirror'],
'Keyless_Entry' => $_POST['keyless_entry'],
'Cruise_Control' => $_POST['cruise_control'],
'Navigation_System' => $_POST['navigation_system'],
'FM_Radio' => $_POST['fm_radio'],
'Cassette_Player' => $_POST['cassette_player'],
'CD_Player' => $_POST['cd_player'],
'Sun_Roof' => $_POST['sun_roof'],
'Alloy_Rims' => $_POST['alloy_rims'],
'HID' => $_POST['hid'],
'Comments' => $_POST['comments'],
'Car_Image' => $pic,
'Display_Name' => $_POST['display_name'],
'Email' => $_POST['email'],
'Mobile_Number' => $_POST['mobile_number'],
'Secondary_Number' => $_POST['secondary_number'],
'Featured' => $_POST['featured']
);
$result_insert_data = $db->insert('car_information',$data);
$msg = '<b> <font face="Verdana, Geneva, sans-serif" color="green">Your Ad has been posted and will be reviewed and displayed within 24hrs</font></b>';
header('Location:Index.php?msg='.$msg);
}
else
{
$msg = '<b> <font face="Verdana, Geneva, sans-serif" color="red">Your Ad has not been posted! Please, try again later</font></b>';
header('Location:postad.php?msg='.$msg);
}
使用 PHP 在 mysql 数据库中插入记录时,验证不适用于文件(已存在/类型)。如您所见,文件函数/验证存在于将数据发布到数据库之前。我想限制重复图像插入/文件类型限制等。虽然我已经编写了代码,但我相信我的 if/else 也存在一些问题。你能帮我吗?