PHP mysql在创建上传图片到blob未定义索引_FILES时出现问题

标签 php mysql blob

我在这里阅读了大约 15 个线程,尝试了每个线程,但没有一个起作用,问题似乎与使用 blob 上传图片有关(是的,我知道我不应该这样做,因为它会缩放,会导致问题但这只是一个原型(prototype))。似乎即使我传递了文件, $_FILES['picture'] 也没有设置。如果我删除 if 并得到类似 $imageName = $_FILES['picture']['name']; 的内容它说索引图片不存在。任何帮助将不胜感激,谢谢。

这是我的 html 代码:

<!DOCTYPE html>
<html>
<head>
<?php include ('header.php'); ?>
<title>Add item</title>
</head>
<body>

<h1>Add item:</h1>

<form method="post" action="additem.php">
Item Name:
<input type="varchar" name="name" maxlength=64>
<br><br>

Description:<br>
<textarea name="description">
</textarea>
<br><br>

Date added:
<input type="date" name="date">
<br><br>

Picture:
<input type="file" name="picture" value="picture">
<br><br>

Shop:
<input type="varchar" name="shop" maxlength=64>
<br><br>

Type:
<input type="varchar" name="type" maxlength=15>
<br><br>

subtype:
<input type="varchar" name="subtype" maxlength=32>
<br><br>

<input type="submit" enctype="multipart/form-data" method="post" name="submit" value="Add event">
</form>
<br><br>

和我的 php 代码:

<?php

if (empty($_SESSION['username'])) {
    echo "Not logged in";
    exit;
}  else {
    if ($_SESSION['privilege'] == "student") {
        echo $_SESSION['username'];
        echo " does not have permission to create events ";
        exit;
    } else {
        echo $_SESSION['username'];
        echo " creating new item";
    }
}

if(!empty($_POST)) {
    require_once('connectdb.php');

    $name = $_POST['name'];
    if(empty($name)) {
        echo(" You must enter an item name.");
        exit;
    }

    $date = $_POST['date'];
    if(empty($date)) {
        echo(" You must enter an item add date.");
        exit;
    }


    $shop= $_POST['shop'];
    if(empty($venue)) {
        echo(" You must enter an shop.");
        exit;
    }

    if (isset($_FILES['picture'])){
    $imageName = $_FILES['picture']['name'];
    $imageData = $_FILES['picture']['tmp_name'];
    $imageType = $_FILES['picture']['type'];

    if(substr($imageType,0,5) == "image")
    {
        echo "this is an image";
    } else {
        echo "Incorrect file type";
        exit;
    }

    $description = $_POST['description'];
    $type = $_POST['type'];
    $subtype = $_POST['subtype'];
    $organiser = $_SESSION['username'];


    try {
        $stmt = $db->prepare("INSERT INTO `items` (`name`, `description`, `date`, `picturename`, `picture`, `organiser`, `shop`, `type`, `subtype`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute(array($name, $description, $date, $imageName, $imageData, $_SESSION['username'], $shop, $type, $subtype));
        echo("Successful.");
    } catch(PDOException $ex) {
        echo("Failed to save data to database.<br>");
        echo($ex->getMessage());
        exit;
    }
    }else{
        echo "file not set";
    }

}

?>
</body>
</html>

最佳答案

您应该为表单指定非默认编码 - 否则浏览器根本不会上传您的文件:

<form method="post" action="additem.php" enctype="multipart/form-data">

关于PHP mysql在创建上传图片到blob未定义索引_FILES时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51693014/

相关文章:

php - 意外的T_CATCH,预期为T_FUNCTION

php - 将内容替换为 SQL 查询的获取参数

php - IN 查询计数随着页面加载速度非常慢,寻找另一个解决方案

php - 表单类型类中的类约束验证器?

PHP MySQLi Stmt 准备

c++ - VS2010 上的 MySQL Connector C 链接问题

mysql - 我可以在 MySQL 的 BLOB 列中放入的最大数据长度是多少?

Windows Azure : Creating a subdirectories inside the blob

javascript - jQuery/JavaScript 从路径文件中读取图像/文件

php - xhpro make 步骤发生错误