doushun1904 2016-09-30 17:27
浏览 31
已采纳

使用php在mysql中插入带有标题和详细信息的照片

I made a page which I can upload an image file but I want to be able to add some details with picture and upload it at the same time and I dont have any idea how can I upload details from a text box with picture in to my table.

Here is my index.php code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="fa-IR">
<head>
<meta name="author" content="www.hamyarprojects.com" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Upload file</title>
<link href="css/Style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="mainframe" > 
    <div id="lp" class = "panel panel-primary">
        <div class = "panel-heading">
            <h3 class = "panel-title">Upload file test</h3>
        </div>
        <div class = "panel-body">
            <form action="Upload.php" method="post" enctype="multipart/form-data">
                <p id="btn_box">
                 <input type="file" name="upload_image" />
                </p>
                <p id="btn_box">
                <input type="submit" value="Upload Photo" name="submit">
                <input type="reset" value="Clear" name="reset">
                </p> 
            </form>
        </div>
    </div>
</div>
</body>
</html>

and here is upload.php code

<link href="css/Style.css" rel="stylesheet" type="text/css">
<div class="main">
<?php

if (!isset($_FILES['upload_image'])) {
    echo '<p>Please select a image</p>';
} else {
    try {
        upload();
        echo '<p>Upload successfully</p>';
    } catch(Exception $e) {
        echo '<h4>'.$e->getMessage().'</h4>';
    }
}

function upload() {
    if (is_uploaded_file($_FILES['upload_image']['tmp_name'])
        && getimagesize($_FILES['upload_image']['tmp_name']) != false
    ) {
        $size = getimagesize($_FILES['upload_image']['tmp_name']);
        $type = $size['mime'];
        $imgfp = fopen($_FILES['upload_image']['tmp_name'], 'rb');
        $size = $size[3];
        $name = $_FILES['upload_image']['name'];
        $maxsize = 99999999;

        if ($_FILES['upload_image']['size'] < $maxsize ) {
            $dbh = new PDO("mysql:host=localhost;dbname=bmc", 'root', '');
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            $stmt = $dbh->prepare("INSERT INTO imageblob (image_type, "
                . "image, image_size, image_name) VALUES (? ,?, ?, ?)");

            $stmt->bindParam(1, $type);
            $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
            $stmt->bindParam(3, $size);
            $stmt->bindParam(4, $name);
            $stmt->execute();
        } else {
            throw new Exception("size invalid");
        }
    } else {
        throw new Exception("invalid format !");
    }
}

</div>
  • 写回答

1条回答 默认 最新

  • dovs36921 2016-09-30 19:29
    关注

    Could you not just add a text box

    <input type="text" name="imageDetails" />
    

    Then in your code capture that text box

    $imageDetails = filter_input(INPUT_POST, "imageDetails", FILTER_SANITIZE_SPECIAL_CHARS);
    

    Then add a column to your table imageBlob called imageDetails

    Then change your query to

    $stmt = $dbh->prepare("INSERT INTO imageblob (image_type ,image, image_size, image_name, imageDetails) VALUES (? ,?, ?, ?, ?)");
    

    Then add the bind param

    $stmt->bindParam(5, $imageDetails);

    Hopefully this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗