gpt4 book ai didi

php - 使用 PHP 在 mysql 数据库中插入记录时验证对文件(已存在/类型)不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 22:06:24 29 4
gpt4 key购买 nike

这是我的 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 也存在一些问题。你能帮忙吗?

最佳答案

看看 documentation for file_exists()并查看它可能失败的一些原因。

  1. file_exists 不适用于指向不存在文件的符号链接(symbolic link)。这可能不是你的问题。
  2. FALSE 对于因 safe mode 而无法访问的文件返回限制。这可能也不是。
  3. 但接下来,请阅读底部的说明:file_exists 的结果已缓存。这可能会导致意外行为。

你会想试试 clearstatcache() function .来自其文档:

You should also note that PHP doesn't cache information about non-existent files. So, if you call file_exists() on a file that doesn't exist, it will return FALSE until you create the file. If you create the file, it will return TRUE even if you then delete the file.

您可能会尝试在 if 语句之前调用以下函数:

clearstatcache(true, "uploads/". $_FILES['file']['name']);
if(file_exists("uploads/". $_FILES['file']['name']))
{
// And so on...

关于php - 使用 PHP 在 mysql 数据库中插入记录时验证对文件(已存在/类型)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17890384/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com