使用php进行表单文件字段验证

时间:2016-02-14 17:12:14

标签: php

我在表单中有一个文件字段,我希望人们上传他们的简历。问题是让它正确验证,似乎即使我添加一个文件,它仍然说文件尚未上传。

这是我的文件字段的表单html。

  function procRegister(){
  global $session, $form;
  /* Convert username to all lowercase (by option) */
  if(ALL_LOWERCASE){
     $_POST['user'] = strtolower($_POST['user']);
  }

  /* Registration attempt */
  $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email'], $_POST['firstname'], $_POST['lastname'], $_POST['telephone'], $_POST['county'], $_POST['job_title'], $_POST['workinindustry'], $_POST['industry_years'], $_POST['salary_range'], $_POST['public'], $_POST['file'], $_POST['postcode'], $_POST['sia_question'], 
  $_POST['sia_license_num'], $_POST['ip1'], $_POST['ip2']);

  /* Registration Successful */
  if($retval == 0){
     $_SESSION['reguname'] = $_POST['user'];
     $_SESSION['regsuccess'] = true;
     $newuser =$_POST['user'];
      $result = mysql_query("UPDATE users SET flinks='$new_file_name' WHERE username='$newuser'") or die(mysql_error()); 
     header("Location: ".$session->referrer);
  }
  /* Error found with form */
  else if($retval == 1){
     $_SESSION['value_array'] = $_POST;
     $_SESSION['error_array'] = $form->getErrorArray();
     header("Location: ".$session->referrer);
  }
  /* Registration attempt failed */
  else if($retval == 2){
     $_SESSION['reguname'] = $_POST['user'];
     $_SESSION['regsuccess'] = false;
     header("Location: ".$session->referrer);
    }
 }

  function register($subuser, $subpass, $subemail, $subfirstname, $sublastname, $subtelephone, $subcounty, $subjob_title, $subworkinindustry, $subindustry_years, $subsalary_range, $subpublic, $subfile, $subpostcode, $subsia_question, $subsia_license_num, $ip1, $ip2){
  global $database, $form, $mailer;  //The database, form and mailer object

  /* Username error checking */
   /* First Name error checking */
  $field = "file";
   if(strlen($subfile) < 2){
     $form->setError($field, "* CV Not Added");
  }
  else{  

  }

以下是PHP

{{1}}

所以这是验证文件的代码,它没有得到这一点,因为它返回错误CV NOT Added,所以它没有得到插入脚本。

1 个答案:

答案 0 :(得分:0)

您的代码中的问题是您使用$_POST Super Global来获取上传文件:

$_POST['file']

在表单中使用input=file时,只能在$_FILES中获取文件值。所以这应该是:

$_FILES['file']

旁注:

还有一点要确保您的文件上传表单具有属性enctype="multipart/form-data",否则文件上传将无效。

<form method="post" enctype="multipart/form-data">

PHP Manual File Uploading