在php中上传图片和数据

时间:2018-11-20 20:06:11

标签: php html

我正在使用php建立一个非常简单的编辑配置文件。

我希望用户能够填充其数据并上传图像。

,但是图像上传不是强制性的。也就是说,用户可以在不上传图像的情况下提交表单,并且用户也可以仅在不填写数据的情况下上传图像(尽管php验证也应检查空白字段和​​错误)。

这是我的html

<form class="form" action="#" method="post" enctype="multipart/form-data" 
   id="registrationForm">
<div class="uploadpic">
   <label class="btn btn-primary" for="my-file-selector">
   <input id="my-file-selector" name="file" class="text-center center-block 
      file-upload" type="file" style="display:none"
      onchange="$('#upload-file-info').html(this.files[0].name)">
   Upload profile photo
   </label>
   <label for="email">
      <h4>Email</h4>
   </label>
   <input type="text" class="form-control" name="email" id="email" value="<?php 
      echo $email ?>">
</div>
<div class="form-group">
   <label for="number">
      <h4>Phone number</h4>
   </label>
   <input type="number" class="form-control" name="number" 
      id="number" value="<?php echo $number ?>">
</div>
<input type="submit" class="btn btn-lg btn-success" name="submit" 
   value="Update">

这是我的php

if (isset($_POST["submit"])) {
    $fileExtensions = ['jpeg', 'jpg', 'png']; // Get all the file extensions
    $fileName = $_FILES['file']['name'];
    $fileSize = $_FILES['file']['size'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileType = $_FILES['file']['type'];
    $fileExtension = strtolower(end(explode('.', $fileName)));
    $uploadPath = $currentDir . $uploadDirectory . basename($fileName);
    $target_file = $uploadDirectory . basename($_FILES["file"]["name"]);
    number = clean($_POST["number"]);
    $email = clean($_POST["email"]);
    if (!in_array($fileExtension, $fileExtensions)) {
        $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG 
file";
    }

    if ($fileSize > 2000000) {
        $errors[] = "This file is more than 2MB. Sorry, it has to be less than or 
equal to 2MB";
    }

    if (empty($email) || strlen($email) < 2) {
        $errors[] = "Input your email address";
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Invalid email address format";
    }

    $query = $pdo->prepare("SELECT email FROM users WHERE email = :email AND id 
!= :sid");
    $query->bindParam(':email', $email);
    $query->bindParam(':sid', $userid);
    $query->execute();
    if ($query->rowCount() > 0) {
        $errors[] = "Email address already registered";
    }

    if (empty($number) || strlen($number) < 10) {
        $errors[] = "Write a valid phone number";
    }

    if (count($errors) == 0) {
        $st = $pdo->prepare("UPDATE users SET email = :email, iname = :iname, ioname 
= :ioname,  number = :number WHERE id = :sid");
        $st->bindParam(':email', $email);
        $st->bindParam(':number', $number);
        $st->bindParam(':iname', $target_file);
        $st->bindParam(':ioname', $fileName);
        $st->bindParam(':sid', $userid);
        $st->execute();
        move_uploaded_file($fileTmpName, $target_file);
    }
    else {
        foreach($errors as $error) {
            $display.= "* $error<br/>";
        }
    }
}

如何

0 个答案:

没有答案