gpt4 book ai didi

php图像和文本上传不起作用

转载 作者:行者123 更新时间:2023-11-30 21:44:33 25 4
gpt4 key购买 nike

我正在尝试使用 php 将一些信息上传到数据库,我已经能够单独上传图像和文本,但不能一起上传(我的目标)。由于某种原因,图像将进入指定的文件夹,但是由于某种原因,信息不会被放入数据库,并且不断向我抛出“失败”的 else 语句!我显然是 php 的新手,我们将不胜感激!这是我的 PHP 代码:

<?php
include_once 'dbh.inc.php';
session_start();

if(isset($_POST['upload'])) {
$file = $_FILES['profileup'];

$fileName = $_FILES['profileup']['name'];
$fileTmpName = $_FILES['profileup']['tmp_name'];
$fileSize = $_FILES['profileup']['size'];
$fileError = $_FILES['profileup']['error'];
$fileType = $_FILES['profileup']['type'];
$Newname = $fileName;
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

$allowed = array('jpg', 'jpeg', 'png');

if (in_array($fileActualExt, $allowed)){

if ($fileError === 0){

if ($fileSize < 10000000000){
$filedest = 'postimages/'.$Newname;
$username = $_SESSION[user];
move_uploaded_file($fileTmpName, $filedest);
header("Location: ../UserProfile.php?suc");
$sql = "INSERT INTO posts (img, ext) VALUES ('$filedest', '$fileActualExt');";
$result = mysqli_query($conn, $sql);
if($result !== false) {
exit;
}else{
echo "fail";
}
}else{
echo "your file was to big";
}

}else{
echo "There was an error uploading your file";
}

}else{
echo "you cant upload files of this type";
}
}else{
header("Location: ../UserProfileSetting.php?fail");
}
?>

最佳答案

    Move your redirection part after you complete your insert query.
The code below will do your trick.
The problem is you r redirecting your page before executing your insert query.
if ($fileSize < 10000000000){
$filedest = 'postimages/'.$Newname;
$username = $_SESSION[user];
move_uploaded_file($fileTmpName, $filedest);
$sql = "INSERT INTO posts (img, ext) VALUES ('$filedest', '$fileActualExt');";
$result = mysqli_query($conn, $sql);
if($result !== false) {
header("Location: ../UserProfile.php?suc");
}else{
echo "fail";
}
}else{
echo "your file was to big";
}

关于php图像和文本上传不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50013427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com