-1

有什么方法可以检查文件上传是否会出错?就像放置一个变量以了解每个 else 语句是否有错误,以便在它阻止运行查询之前。因为即使在移动/复制/删除文件时出现错误,它仍然会运行查询。

$dir = './uploads/images/';

if ($_FILES['thumbnail']['size'] == 0 && $_FILES['thumbnail']['error'] > 0){
        $new_path = $dir;
        $new_file = $new_path . $exist_tn;

        $move = rename($cur_file,$new_file);
        if ($move) {
            $thumbnail = $exist_tn;
        } else {
            echo "Error Moving File.";
        }
        
    }else{      
        $thumbnail = $thumb;
        $deletefile = unlink($cur_file);
        if($deletefile){
            $targetPath = $dir.$thumbnail;
            $sourcePath = $_FILES['thumbnail']['tmp_name'];
            $movefile = move_uploaded_file($sourcePath,$targetPath);
            if ($movefile) {
                echo "File upload successfully.";
            }else{
                echo "Upload File Failed.";
            }

        }else{
            echo "Delete File failed.";
        }
    }

        $query = '
            UPDATE
                `mytbl` 
            SET 
                `title` = "'.$title.'",
                `thumbnail` ="'.$thumbnail.'",
                `date` = "'.$date.'",
                `part` = "'.$part.'",
                `tags`= "'.$tags.'" 
            WHERE
                `id` = "'.$id.'"
            ';

            $update = mysqli_query(db(),$query);
            //Check if there's an error first then run query
            if (checkCondition) {
                if($update === TRUE){
                    echo "sql_update";
                }else{
                    echo "sql_error";
                }
            } else {
                echo "Error Found";
            }
4

1 回答 1

0

如果您不希望查询运行,请在 echo 后使用 exit,例如 $dir = './uploads/images/';

if ($_FILES['thumbnail']['size'] == 0 && $_FILES['thumbnail']['error'] > 0){
        $new_path = $dir;
        $new_file = $new_path . $exist_tn;

        $move = rename($cur_file,$new_file);
        if ($move) {
            $thumbnail = $exist_tn;
        } else {
            echo "Error Moving File.";
        exit();
        }
        
    }else{      
        $thumbnail = $thumb;
        $deletefile = unlink($cur_file);
        if($deletefile){
            $targetPath = $dir.$thumbnail;
            $sourcePath = $_FILES['thumbnail']['tmp_name'];
            $movefile = move_uploaded_file($sourcePath,$targetPath);
            if ($movefile) {
                echo "File upload successfully.";
            }else{
                echo "Upload File Failed.";
                exit();
            }

        }else{
            echo "Delete File failed.";
      exit();
        }
    }

        $query = '
            UPDATE
                `mytbl` 
            SET 
                `title` = "'.$title.'",
                `thumbnail` ="'.$thumbnail.'",
                `date` = "'.$date.'",
                `part` = "'.$part.'",
                `tags`= "'.$tags.'" 
            WHERE
                `id` = "'.$id.'"
            ';

            $update = mysqli_query(db(),$query);
            //Check if there's an error first then run query
            if (checkCondition) {
                if($update === TRUE){
                    echo "sql_update";
                }else{
                    echo "sql_error";
                }
            } else {
                echo "Error Found";
            }
于 2021-03-02T12:43:22.800 回答