我有一个用于上传图像的 php 文件,将其重命名,使其具有独特的值,可以为其他用途获取,上传部分有效,但是当我使用变量 ($fileNewName) 并尝试将其插入数据库时,它不起作用。
session_start();
include_once('dbh-inc.php');
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['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);
$sql = "UPDATE stenden_users SET userImagePath = ".$fileNameNew." WHERE userID =" .$_SESSION['u_id'] ;
if ($stmt = mysqli_prepare($conn,$sql)) {
$result = mysqli_stmt_execute($stmt);
if($result == FALSE){
echo "failed";
} else {
echo "success!!!";
}
}
echo "Your image was successfully uploaded! ";
} else {
echo "The size of file is too big";
}
} else {
echo "There was an error uploading your file";
}
} else {
echo "You cannot upload files of this type";
}
您的 SQL 语法不正确,您可以将 SQL 查询更改为:
$sql = "UPDATE stenden_users SET userImagePath = '".$fileNameNew."' WHERE userID =" .$_SESSION['u_id'] ;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句