插入照片并获取照片

时间:2016-11-10 15:08:50

标签: php

您好,我希望当用户在我的数据库中注册默认照片上传时,我现在可以上传它,但我不知道它是否正确,这里是代码:

 <?php
 require_once 'includes/header.php';

  $profile_pic = basename('files/img/default.jpg');

 if (Input::exists()) {
 //also here we check our token to see if input
if (Token::check(Input::get('token'))) {
//new instance
$validate = new Validate();
//now here we check our validation with check method
//fist of all we check if its POST then:
$validation = $validate->check($_POST,array(
//contain all rulles we want

//first of all in username as we set in our DB
'username'  => array(
  //its required
   'required' => true,
   //minimum character 2
   'min'      => 2,
   //maximum character 20
   'max'      => 20,
   //and we want it unique on users table
   'unique'   => 'users'
),
'name'      => array(
  'required' => true,
  //minimum character 2
  'min'      => 2,
  //maximum character 20
  'max'      => 50,
),
'email'      => array(
  'required' => true,
  //minimum character 2
  'min'      => 10,
  //maximum character 20
  'max'      => 50,
),
'password' => array(
  'required' => true,
  //minimum character 2
  'min'      => 6,
),
'SecondPassword' => array(
  'required' => true,
  //check for be match with password1
  'matches'  => 'password'
 ),


  ));
 //is passed
if($validation->passed()){
  $user = new User();
  $salt = Hash::salt(32);

  try {
    $user->create(array(
      'username' => Input::get('username') ,
      'name' => Input::get('name') ,
      'email' => Input::get('email') ,
      'password' => Hash::make(Input::get('password'),$salt) ,
      'salt' => $salt,
      'joined' => date('Y-m-d H:i:s') ,
      'lastlogin' => date('Y-m-d H:i:s'),
      'avatar' => $profile_pic

    ));
    Session::flash('home','<span class="success-sign">Thank you for your      
     regsiteration Please log in</span> ');
    Redirect::to('index.php');

  } catch (Exception $e) {
    die($e->getMessage());
  }

  }else {
  //if error
  foreach ($validation->errors() as $error) {
    echo "<span class='error-sign'>$error</span><br>";
          }
      }
    } 
  }
    ?>

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

 <label for="username" class='field-name'>Username</label><br>
<!--here in value we declare it to use get method from Input class to get
username for when we see error we dont miss this value and also for name -->
 <input type="text" name="username" id="username"
     value="<?php echo Input::get('username');?>" autocomplete="off"
      onBlur ='checkUser(this)' class='signup'>
     <span id='info'></span>
<br>

  <label for="name" class='field-name'>Your name</label><br>
  <input type="text" name="name"
     value="<?php echo Input::get('name');?>" id="name" class='signup' >
 <br>

<label for="email" class='field-name'>Your Email</label><br>
<input type='email' name='email' id="email" value="<?php echo    
Input::get('email'); ?>"
class='signup'><br>


  <label for="password1" class='field-name'>Password</label><br>
  <input type="password" name="password" id="password" class='signup'>
  <br>

  <label for="secondpassword" class='field-name-re'>Retype Password</label> 
   <br>
  <input type="password" name="SecondPassword" id="SecondPassword"         
  class='signup'> 
  <br>

 <!--this is for token value-->
<input type="hidden" name="token" value="<?php echo Token::generate();?>">
<br>
<span class='fieldname'>&nbsp;</span>
<input type="submit" value="SignUp" class='info-signup'>
</form>
<br>

现在我说我在我的数据库中看到default.jpg在用户注册时存在,但首先我不知道它的照片或照片的名称和第二个我不能在用户个人资料页面中显示这张照片< / p>

2 个答案:

答案 0 :(得分:0)

您永远不想将Image的实际数据实际保存到数据库中。 话虽如此,您的代码将Path名称存储到数据库中,就像它应该如何完成一样。

仔细检查您是否使用了正确的路径。

使用

<img src="<?php echo $your_avatar_url; ?> />

输出图像。

了解如何让用户在此处上传图片 http://www.w3schools.com/php/php_file_upload.asp

答案 1 :(得分:0)

您需要实际保存已张贴的照片。查看move_uploaded_file功能。