1
我正在制作一个网站,人们可以上传个人资料图像。该页面重定向,就好像没有错误,并且SQL更新正常,但该图像不会出现在其目录中。 php.ini表示它有权上传。此外,当我试图在本地主机上同样的事情,它的工作完美PHP上传图像不做任何事,没有给出错误
的HTML:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file" id="file">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
的PHP:
<?php
session_start();
include_once 'dbh.php';
$id = $_SESSION['userID'];
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
//about the 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');
//checks if it's in the allowed type
if (in_array($fileActualExt, $allowed)) {
//checks for errors
if ($fileError === 0) {
//checks for appropriate file size
if ($fileSize < 1000000) {
$fileNameNew = "profile".$id.".".$fileActualExt;
$fileDestination = 'uploads/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
$sql = "UPDATE users SET profilePicture='$fileNameNew' WHERE userID='$id';";
$result = mysqli_query($conn, $sql);
header("Location: index.php?uploadsuccess");
} else {
echo "Your file is too big!";
}
} else {
echo "There was an error uploading your file!";
}
} else {
echo "You must choose a valid file type!";
}
}
'的error_reporting(E_ALL); ini_set('display_errors','On');'在脚本的顶部可能会显示'move_uploaded_file'失败以及原因。 – drew010
@ drew010,这导致我的解决方案,谢谢! – Sebastian
很高兴帮助你!该技巧将为您节省将来无数小时的时间,并在开发过程中遇到问题时始终记住它。 – drew010