我正在尝试制作一个允许用户添加个人资料图片的网站。因此,我有这个upload.php:
<?php
if (!isset($_SESSION)) {
session_start();}
include_once 'includes/dbh.inc.php';
$id = $_SESSION['key'];
if (isset($_POST['submitFile'])) {
$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');
if (in_array($fileActualExt, $allowed)) {
if ($fileError == 0) {
if ($fileSize < 1000000) {
$fileNameNew = "profile".$id.".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
$resultImg = mysqli_query($conn, $sql);
header("Location: home.php?upload=success");
}
else { echo "Sorry, your file size is too big.";}
}
else { echo "Oops, an error occurred!";}
}
else { echo "Please upload png and jpg files only.";}
}
我需要在 home.php 中使用 $fileActualExt。这是代码:
$sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
$resultImg = mysqli_query($conn, $sqlImg);
$rowImg = mysqli_fetch_assoc($resultImg);
if ($rowImg['status'] == 0) {
echo "<img src='uploads/profile".$id.".$fileActualExt'".mt_rand()." height='200' width='200'>";}
else{
echo "<img src='uploads/defaultprofilepicture.jpg' height='200' width='200'>";
}
echo $username;
我需要将upload.php 中的文件扩展名插入到我的home.php 中的img src。
在 home.php 的顶部,我有 session_start() 并包含“upload.php”
当我加载我的网站时,它会显示个人资料图片,我认为它不会。出现“未定义变量:fileActualExt”的错误