-3

我在 DB mysql 上传图像时遇到问题,例如 blob 类型,我将包含脚本 db 以及表单代码和 php 上传文件

sql脚本

   CREATE TABLE IF NOT EXISTS `images` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `data` longblob NOT NULL,   `descripcion` varchar(240) COLLATE utf8mb4_spanish2_ci NOT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_spanish2_ci AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.html(上传表单)

<html>
    <head>

    </head>

    <body>
        <form method="post" action="insertar.php" enctype="multipart/form-data">
        <label>elige imagen:</label>
        <br/>
        <input type="file" name="imagen"/>
        <br/>
        <label>descripcion:</label>
        <br/>
        <textarea cols="20" rows="10" name="texto"></textarea>
        <br/>
        <input type="submit" value="enviar"/>
        </form>
    </body>
</html>

insertar.php(上传代码)

<?php
    include "conexion.php";


    $archivo=$_FILES['imagen']['name'];
    $texto=$_POST['texto'];
    $id=rand(1,200);

    $insertar=mysql_query("INSERT INTO image VALUES('".$id."','".$archivo."','".$texto."')");

    if($insertar){
        echo "<a href='verImagenes.php'> ver mis imagenes</a>";
    }else{
        echo "fallo la insercion";
    }
?>

这是错误消息:

注意:第 9 行 C:\xampp\htdocs\ejemlo\insertar.php 中的数组到字符串转换

4

1 回答 1

1

为什么要尝试将 *$_FILES* 数组存储在数据库中?*$_FILES* 是一个数组,用于存储有关您刚刚上传的文件的信息,因此会出现数组到字符串错误。

当您上传文件时,该文件会临时存储在服务器上。您需要正确处理文件上传 - 即将其保存在某处,然后将此路径存储在您的数据库中。

您总是可以序列化 *$_FILES* 数组并存储它,但我不知道这有什么用,因为它只引用临时在您的服务器上的文件。

于 2013-04-25T01:44:54.293 回答