PHP IF语句中的未知错误行为

时间:2013-05-23 15:17:41

标签: php if-statement behavior is-uploaded-file

我正在编写一个用PHP上传文件的代码。但是在它的IF声明中有一个未知和奇怪的问题。它在真实和错误的条件下进行操作!请看下面的代码:

if (is_uploaded_file($_FILES['catalogue']['tmp_name']))
{
    $ext = find_extension('catalogue');
    $ext_array = array('pdf');

    if (!in_array($ext,$ext_array))
    {
        // echo something for error message.
    }
    else
    {
        echo ' Step1 ';
        @unlink ('../../catalogues/'.$id.'.pdf');
        if(@move_uploaded_file($_FILES['catalogue']['tmp_name'],"../../catalogues/".$id.'.pdf'))
        {
            @chmod ("../../catalogues/".$id.".pdf",'644');
            $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
            $q = $db->prepare($sql);
            $query = $q->execute(array($id.'.pdf',$id));
        }
    }
}
else
{
    echo ' Step2 ';
    @unlink ('../../catalogues/'.$id.'.pdf');
    $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
    $q = $db->prepare($sql);
    $query = $q->execute(array('',$id));
}

结果是 Step1 Step2
因此,当文件上传成功时,新文件将被上传并设置为数据库,然后它将在步骤2中删除,其数据库中的字段将为空。 :(
这对我来说很奇怪。请帮帮我。

注意:find_extension()函数和$id在这些代码行之前定义。

1 个答案:

答案 0 :(得分:0)

试试这个:

if (is_uploaded_file($_FILES['catalogue']['tmp_name']))
{
    $ext = find_extension('catalogue');
    $ext_array = array('pdf');

    if (!in_array($ext,$ext_array))
    {
        // echo something for error message.
    }
    else
    {
        echo ' Step1 ';
        @unlink ('../../catalogues/'.$id.'.pdf');
        if(@move_uploaded_file($_FILES['catalogue']['tmp_name'],"../../catalogues/".$id.'.pdf'))
        {
            @chmod ("../../catalogues/".$id.".pdf",'644');
            $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
            $q = $db->prepare($sql);
            $query = $q->execute(array($id.'.pdf',$id));
     }
}
else
{
    echo ' Step2 ';
    @unlink ('../../catalogues/'.$id.'.pdf');
    $sql = "UPDATE tbl_products SET catalogue = ? WHERE id = ?";
    $q = $db->prepare($sql);
    $query = $q->execute(array('',$id));
}

    }