使用表单上传图像

时间:2012-04-18 18:26:46

标签: php

我一直遇到使用此注册表单上传图片的用户的问题:

<?php
errors = array();
$missing = array();
// check if the form has been submitted
if (isset($_POST{'submit'}) && !empty($_FILES)){
    require_once('./classes/Ps2/Upload.php');
 try {
    $upload = new Ps2_Upload('my address');
    $upload->move();
    $messages = $upload->getMessages();
  }catch (Exception $e) {
    echo $e->getMessage();
  }

   $tmp_name = $_FILES['image']['tmp_name'];
   $type = $_FILES['image']['type'];
   $name = $_FILES['image']['name'];
  $size = $_FILES['image']['size'];
   $names = $_POST['names'];
   $number = $_POST['number'];
   $email = $_POST['email'];
   $seminar = $_POST['seminar'];
   $address = $_POST['address'];

$response = recaptcha_check_answer($private_key, $_SERVER['REMOTE_ADDR'],       $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if (!$response->is_valid) {
$errors['recaptcha'] = true;
}
<?

这是表格(至少影响图片上传的部分):

<form id="register" name="register" method="post" enctype="multipart/form-data" action="">
  <p>
  <fieldset><legend class="caption">Registration Details</legend>
    <p>
      <label for="image">Upload image photograph:
      <?php if ($missing && in_array('image', $missing)) { ?>
<span class="warning">Please pick your passport</span>
<?php } ?>
      </label>
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>">
    <input name="image" id="image" type="file" value="image" tabindex="1">
      </p>

1 个答案:

答案 0 :(得分:0)

如果您要做的是确保用户不会跳过选择要上传的文件,那么您可以使用Javascript进行检查:

<script type="text/javascript">
    function checkImage()
    {
        if (document.getElementById('image').value == "")
        {
            alert("Please select an image to upload.");
            return false;
        }

        return true;
    }
</script>

<form onsubmit="return checkImage()" ... >

如果那不是您的意思,请澄清您所面临的问题。