duanfan5012 2016-05-20 07:17
浏览 38

需要帮助上传图像到PHP

I would like to create a php form with image upload to database by store image path to database and store the image to your server or directory. I'm kind of not sure how to do image upload.

Database: dbtest

CREATE TABLE IF NOT EXISTS `tbl_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fullName` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  'pic' blob NOT NULL
  PRIMARY KEY (`id`),

PHP:

<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();

if(!$user_home->is_logged_in())
{
  $user_home->redirect('index.php');
}


if(isset($_POST['btn-register']))
{
  $fullName = $_POST['fullName'];
  $email = $_POST['email'];


  $stmt = $user_home->runQuery("SELECT * FROM bike WHERE id=:id");
  $stmt->execute(array(":id"=>$_SESSION['userSession']));
  $row = $stmt->fetch(PDO::FETCH_ASSOC);;

  if($reg_user->register($email,$fullName))
  {



    $message = "
            Thank You for registering  with us!<br/>
            <br />
            Thanks,<br/>
            <br />
            Site Admin";

    $subject = "Confirm Registration";
  }
  else
  {
    echo "sorry , query could no execute.";
  }
}

?>

<!DOCTYPE html>
<html class="no-js">

    <head>
        <title><?php echo $row['email']; ?></title>
        <!-- Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../css/NewFile.css">


    </head>

    <body>
      <script src="../js/jquery-1.12.3.min.js"></script>

  <script src="../js/bootstrap.min.js"></script>

   <?php include 'navBarLogin.php'; ?>

        <?php if(isset($msg)) echo $msg;  ?>

      <form method="POST" action="" enctype="multipart/form-data">


        <h2> Bicycle Register</h2>
        <hr>
        <table>

        <tr>
        <td>Full name:</td>
        <td><input type="text" class="input-block-level"  name="fullName"  /></td>
        </tr>


        <tr>
        <td>email:</td>
        <td><input type="text" class="input-block-level"  name="email"  /></td>
        </tr>



             <tr>
             <td>SPicture:</td>
            <td><input type="file"  name="pic" required />
             </tr>

             </table>
  <button class="btn btn-large btn-primary" type="submit" name="btn-register">Register</button>

   </form>

    </body>

</html>

website created base on http://www.codingcage.com/2015/09/login-registration-email-verification-forgot-password-php.html

  • 写回答

3条回答 默认 最新

  • dslpofp041310584 2016-05-20 07:34
    关注

    After this line:

    $email = trim($_POST['email']);
    

    Add this piece of code for the image upload:

    ...

    if(isset($_FILES['pic'])){
          $errors= array();
          $file_name = $_FILES['pic']['name'];
          $file_size = $_FILES['pic']['size'];
          $file_tmp = $_FILES['pic']['tmp_name'];
          $file_type = $_FILES['pic']['type'];
          $file_ext=strtolower(end(explode('.',$_FILES['pic']['name'])));
    
          $expensions= array("jpeg","jpg","png");
    
          if(in_array($file_ext,$expensions)=== false){
             $errors[]="extension not allowed, please choose a JPEG or PNG file.";
          }
    
          if($file_size > 2097152) {
             $errors[]='File size must be excately 2 MB';
          }
    
          if(empty($errors)==true) {
             move_uploaded_file($file_tmp,"images/".$file_name);
             echo "Success";
          }else{
             print_r($errors);
          }
       }
    

    ...

    And then you have to insert the $file_name in your DB.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化