【发布时间】:2017-12-06 09:29:41
【问题描述】:
我已经有一个合适的图片上传系统。它将图片上传并存储在文件夹/uploads/ 中,现在我需要跟踪上传个人资料图片的人员,并且我想在我的用户个人资料页面上显示该图片。
这是我的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>
为此,我有 2 个表,一个处理用户名、fname、lname、电子邮件、密码、用户描述等。我希望第二个显示他们的个人资料图像的状态,即如果他们上传了图像, status 将为 1,如果没有,则 status 为 0。如果 status 为 0,则将显示来自目录 /uploads/profiledefault.jpg 的图像,这是新用户的默认配置文件图像。当谈到 PHP 时,我仍然是初学者。希望有人能告诉我这里的路。
【问题讨论】:
标签: php mysql image file-upload