1 回答
TA贡献1798条经验 获得超3个赞
解决图片上传条件,不覆盖已有图片文件:
<?php
require 'db.php';
session_start();
$uploadDir = '../profiles/';
// if edit-picture has been clicked on, run this if statement
if (isset($_POST[ 'edit-picture' ])) {
$studentID = $_SESSION[ 'studentID' ];
// Creating 4 different variables using the $_FILES global variable to get data about the image that
// you can view data about a file by doing: print_r($image);
$fileName = $_FILES[ 'image' ][ 'name' ];
$tmpName = $_FILES[ 'image' ][ 'tmp_name' ];
$fileSize = $_FILES[ 'image' ][ 'size' ];
$fileType = $_FILES[ 'image' ][ 'type' ];
$filePath = $uploadDir . $fileName;
// The below doesn't work as it assigns different value to folder and in db for image name
// $filePath = md5($file_name . microtime()) . substr($fileName , -5, 5);
if (file_exists($filePath)) {
header("Location: ../profile.php?img=errorFileNameExists");
exit();
} // Checking file size - working
else if ($_FILES[ "image" ][ "size" ] > 5000000) {
header("Location: ../profile.php?img=errorFileSizeError");
exit();
}
$info = getimagesize($tmpName);
// empty $info - not known image
if (empty($info)) {
header("Location: ../profile.php?img=errorFileTypeError");
exit();
} // This is to show any errors that may occur if the connection fails, this helps with error checking.
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
header("Location: ../profile.php?img=errorFileRelocate");
exit;
}
else if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$stmt = $conn->prepare("INSERT INTO `profileImage` (`imagePath`, `studentID`)
VALUES ( ?, ?) ON DUPLICATE KEY UPDATE `imagePath` = VALUES (`imagePath`) ");
$stmt->bind_param("si", $fileName, $studentID);
$stmt->execute() or die("Failed to insert image into the database");
header("Location: ../profile.php?img=successImageUploaded");
exit();
}
}
?>
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报