-2

我正在使用php开发注册表单,我需要上传文件而不是上传

$firstname=$_REQUEST['firstname'];
$lastname=$_REQUEST['lastname'];
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
$email=$_REQUEST['email'];
$Image=basename($_FILES['file']['name']);
$Image=str_replace(' ','|',$Image);

if($firstname && $lastname && $username && $email)
{
  require 'dbconnect.php';
  $query="select * from members where email='$email'";
  $result=mysql_query($query);
  $exists=mysql_num_rows($result);
  if($exists>0)
  {
    $message=array("message"=>"email already existed");
    echo json_encode($message);
  }
  else
  {
    if($Image)
    { 
      $Image=date("YmdHis").".jpg";    
      $target_path="userimages/".$Image;
      move_uploaded_file($_FILES["file"]["tmp_name"],$target_path);
      $query="INSERT INTO `members` 
        (`username`, `firstname`, `lastname`, `email`, `password`, `image`)  VALUES 
        ('$username', '$firstname', '$lastname', '$email', '$password',  '$Image')";
    }
    else
    {
      $query="INSERT INTO `members` (`username`, `firstname`, `lastname`, `email`, `password`)  VALUES 
       ('$username', '$firstname', '$lastname', '$email', '$password')";
    }
    $result=mysql_query($query);
  }
}

html代码是

<html>
<form action="register.php" method="post" enctype="multipart/form-data">
<div>firstname<input type="text" name="firstname"></div>
<div>lastname<input type="text" name="lastname"></div>
<div>username<input type="text" name="username"></div>
<div>password<input type="text" name="password"></div>
<div>email<input type="text" name="email"></div>
<div>image<input type="file" name="file"></div>
<div><input type="submit" name="submit"></div>
</form>
</html>

在这个我上传图片但它没有上传到服务器,也没有上传到数据库,在这种情况下被执行

请帮我

4

2 回答 2

2

确保您的<form>标签具有该enctype="multipart/form-data"属性

于 2012-06-25T19:15:51.547 回答
0

检查您尝试写入的文件夹的权限 - 如果图像未上传且move_uploaded_file失败,则该行根本不会插入数据库。

为了比较,以下是我用来将文件上传到服务器的应用程序的一些代码:

if ((($_FILES["avatar"]["type"] == "image/gif")
    || ($_FILES["avatar"]["type"] == "image/jpeg")
    || ($_FILES["avatar"]["type"] == "image/pjpeg")
    || ($_FILES["avatar"]["type"] == "image/png"))
    && ($_FILES["avatar"]["size"] < 200000))
{
    if ($_FILES["avatar"]["error"] > 0)
    {
        $error =  "Return Code: " . $_FILES["avatar"]["error"] . "<br />";
    }
    else
    {
        $fname = time() . '-' . $formData['username'] .$_FILES["avatar"]["name"];
        $fname = str_replace(" ", "_", $fname);
        if (file_exists("/myserverdir/images/avatars/" . $fname))
        {
            $fname = time() . '-' . $formData['username'] .$_FILES["avatar"]["name"];
            $fname = str_replace(" ", "_", $fname);
        }
        move_uploaded_file($_FILES["avatar"]["tmp_name"], "/myserverdir/images/avatars/" . $fname);
    }
}
else if (!file_exists($_FILES['avatar']['tmp_name']) || !is_uploaded_file($_FILES['avatar']['tmp_name']))
{
    $fname = 'default_avatar.jpg';
}
else
{
    $error = "Invalid file";
}

我的表格看起来像:

<form method="post" action="myaction.php" enctype="multipart/form-data">
    <input type="file" name="avatar" />
    <input type="text" name="username" />
</form>
于 2012-06-25T19:14:59.557 回答