2017-07-03 50 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,并且不需要检查它。

相关问题