1

我有下一个问题:验证姓名、地址和电子邮件的代码有效,但验证图像无效,不知道为什么会发生这种情况。一如既往地打印“选择图像”

我做了一个测试,并设置了用“print $photo”打印图像的临时名称但没有打印出来的行!

表单.php

<form id="formregister" name="formregister" action="register.php" method="post">
    <input id="name" name="name" type="text"/>
    <input id="address" name="address"  type="text"/>
    <input id="email" name="email" type="email" />
    <input id="photouser" name="imguser" type="file"/>
    <input id="bregister" name="bregister" type="submit" value="Registrer"/>
</form>

注册.php

..............
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$photo = $_FILES['imguser']['tmp_name'];
$errors = array();

    if(!isset($photo)) {
        $errors[] = "Select an image";
    } else {
        $fototemp = addslashes(file_get_contents($_FILES['imguser']['tmp_name']));
        $fotoname = addslashes($_FILES['imguser']['name']);
        $fotosize = getimagesize($_FILES['imguser']['tmp_name']);
        if ($fotosize == false) {
            $errors[] = "Invalid format";
        }
    } 
//Code to validate the other fields (name, address, email)
....................
4

1 回答 1

4

您在表单上缺少正确enctype的文件上传。它应该是enctype='multipart/form-data'

<form id="formregister" name="formregister" action="register.php" method="post" enctype='multipart/form-data'>

与其直接访问辅助键,不如tmp_name先测试它是否被设置以避免未定义的索引通知:

if (isset($_FILES['imguser']) {
  $photo = $_FILES['imguser']['tmp_name'];
}
于 2012-06-16T22:27:17.817 回答