gpt4 book ai didi

php - 无法使用php上传图片

转载 作者:行者123 更新时间:2023-11-29 00:03:22 25 4
gpt4 key购买 nike

我正在尝试使用 php 上传图片;但似乎不起作用,我不明白为什么。当我尝试只插入没有图像的文本时,它工作正常(我的意思是如果我从 php 部分删除图像上传的所有代码);所以我猜错误在这部分,但我找不到它。这是我目前使用的上传代码。对此的任何帮助表示赞赏。

if (isset($_POST['add'])) {

$text = $_POST['text'];
$title = $_POST['title'];
$category = $_POST['category'];

$fileName = $_FILES['userfile']['userfile'];
$tmpName = $_FILES['userfile']['tmp_name'];

// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());

// image name with extension
$myFile = $randName . '.' . $ext;
// save image path
$path = "/img/" . $myFile;

$result = move_uploaded_file($tmpName, $path);

if (!$result) {
echo "Error uploading image file <br />";
var_dump($_FILES);
exit;
} else {
$db = new mysqli("localhost", "user", "mypass", "mydb");

if (mysqli_connect_errno()) {
printf("Connect failed: %s<br/>", mysqli_connect_error());
}
mysqli_set_charset($db, "UTF8");

$query = "INSERT INTO posts (post_text, image_name, post_image, post_title, category) VALUES (?, ?, ?, ?, ?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
$conn->bind_param("ssssi",$text, $myFile, $path, $title, $category);
if (!$conn->execute()) {
echo 'error insert';
} else {

header("Location: index.php");
exit;
}
} else {
die("Error preparing Statement");
}
}
} else {
echo 'error';
}

这是上传的表格

<form method="post" action="postblog.php" enctype="multipart/form-data">
Title: <input type="text" name="title" id="title">
Category: <select name="category" id="category">
Desc :<br />
<textarea id="text" name="text" rows="15" cols="80" style="width: 80%"></textarea>
Image: <input type="file" name="userfile" />
<input type="submit" name="add" id="add" value="Добави">
</form>

错误来自这里:

if (!$result) { echo "Error uploading image file
"; }

最佳答案

改变 $fileName = $_FILES['userfile']['userfile'];$fileName = $_FILES['userfile']['name'];

关于php - 无法使用php上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28675390/

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