我正在制作一个网站,人们可以在其中上传个人资料图片。页面重定向就好像没有错误一样,并且 SQL 更新正常,但图像没有出现在它的目录中。 php.ini 说它有上传权限。此外,当我在本地主机上尝试同样的事情时,它运行得很好
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!";
}
}
最佳答案
检查上传目录的路径。
$path = getcwd();
echo $path;
$fileDestination = 'uploads/'.$fileNameNew;
可能最终看起来像
$fileDestination = '/home/user/public_html/uploads/'.$fileNameNew;
关于PHP上传图片什么都不做,没有报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42706621/