-2

我正在编写代码以在 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定义在这些代码行之前。

4

1 回答 1

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));
}

    }
于 2013-05-23T15:20:57.570 回答