3

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

4

1 回答 1

0

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

  1. file_exists不适用于指向不存在文件的符号链接。这可能不是你的问题。
  2. FALSE为由于安全模式限制而无法访问的文件返回。这大概也不是。
  3. 但接下来,请阅读底部的注释:结果file_exists被缓存。这可能会导致意外行为。

你会想试试这个clearstatcache()功能。从其文档中:

您还应该注意,PHP 不会缓存有关不存在文件的信息。因此,如果您调用file_exists()一个不存在的文件,它将返回 FALSE,直到您创建该文件。如果您创建该文件,即使您随后删除该文件,它也会返回 TRUE。

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

clearstatcache(true, "uploads/". $_FILES['file']['name']);
if(file_exists("uploads/". $_FILES['file']['name']))
{
    // And so on...
于 2013-08-03T16:13:02.103 回答