我在向用户展示错误信息的方式上遇到了一些问题。我通过使用两个Goto指令来“解决”这个问题。请看代码:
<?php require_once("registration/include/membersite_config.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head><?php include_once("parts/head.php"); ?></head>
<body>
<div id="footerfix">
<?php include_once("parts/header.php"); ?>
<div class="container">
<div class="hero-unit">
<?php
if (isset($_GET['i'])) {
unlink("users/thumbs/" . $_SESSION["user_code"] . ".jpg");
header('Location: profile.php?i=mycv');
}
if (isset($_FILES['avatar']['tmp_name'])) {
$file_ext = end(explode('.', $_FILES['avatar']['name']));
if (in_array($file_ext, array('jpg', 'jpeg', 'png', 'gif')) == false) {
echo("<h2>Error!</h2><p>Your profile photo have to be a picture file.</p>");
goto nomore;
}
$src_size = getimagesize($_FILES['avatar']['tmp_name']);
if ($src_size['mime'] == 'image/jpeg') {
$src_img = imagecreatefromjpeg($_FILES['avatar']['tmp_name']);
} elseif ($src_size['mime'] == 'image/png') {
$src_img = imagecreatefrompng($_FILES['avatar']['tmp_name']);
} elseif ($src_size['mime'] == 'image/gif') {
$src_img = imagecreatefromgif($_FILES['avatar']['tmp_name']);
} else {
echo("<h2>Error!</h2><p>Incorrect file format.</p>");
goto nomore;
}
$thumb_w = 150;
if ($src_size[0] <= $thumb_w) {
$thumb = $src_img;
} else {
$new_size[0] = $thumb_w;
$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_w;
$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
imagejpeg($thumb, "users/thumbs/" . $_SESSION["user_code"] . ".jpg");
//header('Location: profile.php?i=mycv');
echo('<h2>Ready!</h2><p>Your profile picture is updated. <a href="profile.php">Go back</a>.</p>');
nomore:
echo "</div></div>";
include_once("parts/footer.php");
echo "</div></body></html>";
}
?>
我从来不明白为什么goto语句是代码中最糟糕的事情(至少,每个人都这么说),我想听听你们对此的看法,如果它真的是有史以来最糟糕的事情,那么如何在没有它们的情况下使用我的代码?谢谢!
goto
和 PHP 中的goto
构造具有极大的不同行为。 - mario