2016-02-07 155 views
0

我是新來的php,並試圖上傳文件並將上載的文件存儲在數據庫中的當前登錄用戶ID,這是我的上傳文件格式:如何獲取用戶ID並將其存儲到數據庫

<?php 
error_reporting(E_ALL^E_NOTICE); 
session_start(); 
?> 
<form method="post" enctype="multipart/form-data"> 
<table width="350" border="0" cellpadding="1" 
cellspacing="1" class="box"> 
<tr> 
<td>please select a file</td></tr> 
<tr> 
<td> 
<input type="hidden" name="MAX_FILE_SIZE" 
value="16000000"> 
<input name="pdf" type="file" id="pdf" accept="application/pdf"> 
</td> 
<td width="80"><input name="upload" 
type="submit" class="box" id="upload" value=" Upload "></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
<?php 
if(isset($_POST['upload'])&&$_FILES['pdf']['size']>0) 
{ 
$fileName = $_FILES['userfile']['name']; 
$tmpName = $_FILES['userfile']['tmp_name']; 
$fileSize = $_FILES['userfile']['size']; 
$filePath = $_FILES['userfile']['path']; 
$fileType = $_FILES['userfile']['type']; 
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile']))); 
$fp  = fopen($tmpName, 'r'); 
$content = fread($fp, filesize($tmpName)); 
$content = addslashes($content); 
fclose($fp); 
if(!get_magic_quotes_gpc()) 
{ 
    $fileName = addslashes($fileName); 
} 
$con = mysql_connect('localhost', 'root', '') or die(mysql_error()); 
$db = mysql_select_db('workflow', $con); 
if($db){ 
$query = "INSERT INTO upload (name, size, type, path ) ". 
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')"; 
mysql_query($query) or die('Error, query failed'); 
mysql_close(); 
echo "<br>File $fileName uploaded<br>"; 
}else { echo "file upload failed"; } 
} 
?> 

,這是我的登錄表單:

<?php 
error_reporting(E_ALL^E_NOTICE); 
session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>LOGIN FORM</title> 
</head> 
<body> 
<?php 
$form= "<form action='login.php' method='post'> 
    <table> 
    <tr> 
     <td>username :</td> 
     <td><input type='text' name='user'/></td> 
    </tr> 
    <tr> 
     <td>Password :</td> 
     <td><input type='password' name='password'/></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td><input type='submit' name='loginbtn' value='login' /></td> 
    </tr> 
    </table> 
</form>"; 
if ($_POST['loginbtn']) { 
    $user = $_POST['user']; 
    $password = $_POST['password']; 
    if ($user) { 
    if ($password) { 
     require ("connect1.php"); 
     $password= md5(md5("jdhbjdbj".$password."jdh645fdj")); 
     $query = mysql_query("SELECT * FROM users WHERE username='$user'"); 
     $numrows = mysql_num_rows($query); 
     if ($numrows == 1) { 
     $row = mysql_fetch_assoc($query); 
     $dbid=$row['id']; 
     $dbuser=$row['username']; 
     $dbpass=$row['password']; 
     $dbactive=$row['active']; 
     if ($password == $dbpass) { 
      if ($dbactive == 1) { 
      $_SESSION['userid'] = $dbid; 
      $_SESSION['username'] = $dbuser; 

      echo "you have been logged in as <b>$dbuser</b>. <a href='insert1.php' >Click here</a> to go to the insertion page "; 

      } 
      else echo "you must activate your account to login .$form"; 
     } 
     else echo "You didn't enter a password . $form"; 
     } 
     else echo "the user name u entered wasn't found . $form";; 
     mysql_close(); 
    } 
    else echo "You must enter your password .$form"; 
    } 
    else echo "You must enter your username .$form"; 
} 
else echo $form; 

?> 
</body> 
</html> 

我想獲得當前用戶ID,並將它與文件存儲我上載她的任何一個可以幫助???

+0

當用戶在你的系統中存儲了他的詳細信息?在會議或其他事情? –

+0

@ A-2-A是的,我打開一個會話,當他登錄 –

+0

上傳是否工作?Shoudnt this'$ _FILES ['userfile'] ['name']'是'$ _FILES ['pdf'] [''名字']'等等? – Mihai

回答

2

您需要添加另一列到你的上傳數據庫,如userid,如果你還沒有,只是把它想:

$query = "INSERT INTO upload (name, size, type, path, userid ) ". 
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '".$_SESSION['userid']."')"; 

$ _SESSION [「用戶ID」] = <用戶ID必須是更改爲保存用戶ID的密鑰。不要忘記調用session_start();

+0

當我對這個$ _SESSION ['userid']和錯誤是:期待標識符(T_STRING)或變量(T_VARIABLE)或數字(T_NUM_STRING)時,它給出了一個錯誤 –

+0

嘗試編輯,確保你調用session_start() ;在頂部,用戶標識實際上保存用戶標識。如果尚未設置,則無法使用。 – AersolKing

+0

我試過你說,它上傳文件,但ID仍然給出爲0,它應該是2 –

0

那麼這裏有一些步驟,建議您遵循: 建議第一:

  1. 使用mysqli_query instead of mysql_queryWhy mysqli?

  2. mysql_real_escape_string()擴展爲deprecated .Read this尋求幫助。

  3. 始終爲數據庫連接維護一個單獨的文件,並將其保留在public_directory之外,以使黑客難以訪問。 How to do it?

現在爲你「想獲得當前用戶ID,並將它與文件存儲」: 要做到這一點,我會做這樣的事情:

  1. 首先,在記錄時間將用戶標識存儲在會話中。 How ?
  2. 存儲在文件系統中的文件:

    if (move_uploaded_file($_FILES['userfile']["tmp_name"],"Filesystem/here/$_FILES['userfile']['name']")) 
    {  
        // get the db connection 
        // get the user_id value from session 
        // store the user_id with path to file or whatever in database 
    } 
    

希望它會幫助你在一定程度上。祝你快樂編碼:-)

相關問題