2017-07-03 49 views
0

我已經有一個適當的圖像上傳系統。它上傳&存儲圖像的文件夾/uploads/的,現在我需要跟蹤誰上傳個人資料圖片,我想我的用戶個人資料頁上顯示圖像。允許用戶上傳PHP中的配置文件圖像

這是我的upload.php的:

<?php 
include('db.php'); 

// Check if user is logged in using the session variable 
if ($_SESSION['logged_in'] != 1) { 
    $_SESSION['message'] = "You must log in before viewing your profile page!"; 
    header("location: error.php");  
} 

if (isset($_POST['submit'])) { 
    $file = $_FILES['file']; 

    $fileName = $file['name']; 
    $fileTmpName = $file['tmp_name']; 
    $fileSize = $file['size']; 
    $fileError = $file['error']; 
    $fileType = $file['type']; 

    $fileExt = explode('.', $fileName); 
    $fileActualExt = strtolower(end($fileExt)); 

    $allowed = array('jpg', 'jpeg', 'png', 'pdf'); 

    if (in_array($fileActualExt, $allowed)) { 
     if ($fileError === 0) { 
      if ($fileSize < 1000000) { 
       $fileNameNew = uniqid('', true).".".$fileActualExt; 
       $fileDestination = 'uploads/'.$fileNameNew; 
       move_uploaded_file($fileTmpName, $fileDestination); 
       header("Location: user.php"); 
      } else { 
       echo "Your file is too big!"; 
      } 
     } else { 
      echo "There was an error uploading your file!"; 
     } 
    } else { 
     echo "You cannot upload files of this type!"; 
    } 
} 

,這是HTML

<form action="upload.php" method="POST" enctype="multipart/form-data" > 
<div class="specialcontainer"> 
    <input type="file" name="file" id="file" class="inputfile"> 
</div> 
    <div class="inner"></div> 
    <button type="submit" name="submit" class="uploadbuttonstyle">Upload</button> 
</form> 
</div> 

我爲此具有兩個表,一個處理的用戶名,FNAME, lname,電子郵件,密碼,用戶描述等。我希望第二個顯示他們的個人資料圖片的狀態,即如果他們上傳了圖片,狀態將爲1,如果他們沒有,那麼狀態將爲0。如果狀態爲0,將顯示目錄/uploads/profiledefault.jpg的圖像,這是新用戶的默認配置文件映像。談到PHP時,我仍然是一名初學者。希望有人能在這裏向我展示。

回答

0

你並不需要使用另一個表這一點。只需在您的第一個表添加更多的列「profile_image」並保存圖像中表

if (move_uploaded_file($fileTmpName, $fileDestination)) { 
// save/update "profile_image" field here. 
} 

,當你要顯示的個人資料圖片只檢查其中profile_image欄是空白的或沒有。如果是,則顯示默認圖像「/uploads/profiledefault.jpg」,否則顯示「profile_image」列中的配置文件圖像。

+0

我猜我將不得不使用UPDATE查詢來更新profile_image列,並且WHERE子句將爲每個用戶(例如他/她的電子郵件)包含一個唯一值? –

+0

絕對正確! – Sehdev

0

我相信你有實體命名的用戶。在此實體中添加屬性profileImage,並在上載後保存圖像的路徑。您獲取當前用戶並將文件路徑添加到其屬性profileImage。每次註冊一個新用戶時,您只需將profileImage指定爲默認圖像/uploads/profiledefault.jpg的路徑即可。這意味着用戶在每一點都會有profileImage,並且不需要檢查它。

相關問題