【问题标题】:save image in xampp and show it in web php将图像保存在 xampp 中并在 web php 中显示
【发布时间】:2018-04-26 05:59:24
【问题描述】:

HTML 上传表单:

<form action="InformationData.php" method="post" enctype="multipart/form-data">
    <label >Barangay Certification</label>
    <input name="BarangayCertification" type="file" id="exampleInputFile1">
    <button type="Submit" name="Submit" value="Upload">Submit</button>
</form>

InformationData.php:

<?php 
$conn = mysqli_connect("localhost", "root", "", "registration");

if($_POST['BarangayCertification']){
    $BarangayCertification =   $_POST['BarangayCertification'];
} else {
    $BarangayCertification =  "";
}

$sql = "INSERT INTO stakeholdersform (BarangayCertification) VALUES ($BarangayCertification);
?>

显示图像的代码:

<?php
$conn = mysqli_connect("localhost", "root", "", "registration");
$informations = "SELECT * FROM stakeholderinformations";
$result = $conn->query($informations);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $BarangayCertification = $row['BarangayCertification']; 
        echo $BarangayCertification;
    }
}
?>

我试图回显它但没有任何反应,但我可以在数据库中看到图像。

【问题讨论】:

  • 问题的状态如何?

标签: php html database xampp


【解决方案1】:

如果与数据库的连接设置正确。下面的代码可以工作。但首先你必须在根目录中创建一个目录上传

表格

<form action="InformationData.php" method="post" enctype="multipart/form-data">
    <label >Barangay Certification</label>
    <input name="BarangayCertification" type="file" id="exampleInputFile1">
    <button type="Submit" name="Submit" value="Upload">Submit</button>
</form>

InformationData.php:

<?php 
$conn = mysqli_connect("localhost", "root", "", "registration");

if (isset($_POST("Submit"))){
if($_POST['BarangayCertification']){
   // $BarangayCertification =   $_POST['BarangayCertification'];
            if (file_exists("upload/" . $_FILES["BarangayCertification"]["name"])) {
            echo $_FILES["BarangayCertification"]["name"] . " <b>already exists.</b> ";
            } else {
                 ///creat upload in root 
            move_uploaded_file($_FILES["BarangayCertification"]["tmp_name"], "upload/" . $_FILES["BarangayCertification"]["name"]);
            $BarangayCertification = "//".$_SERVER['HTTP_HOST'].'//'. "upload/" . $_FILES["file"]["name"];

            }
} else {
    $BarangayCertification =  "";
}
}
$sql = "INSERT INTO stakeholdersform (BarangayCertification) VALUES ($BarangayCertification)";
?>

显示图像的代码:

<?php
$conn = mysqli_connect("localhost", "root", "", "registration");
$informations = "SELECT * FROM stakeholderinformations";
$result = $conn->query($informations);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $BarangayCertification = $row['BarangayCertification']; 
        echo "<img src=".$BarangayCertification.">";
    }
}
?>

【讨论】:

    【解决方案2】:

    来自PHP documentation

    echo — 输出一个或多个字符串

    所以,不,您不能回显图像。 你能做的就是

    echo '&lt;img src="data:image/jpeg;base64,' . $BarangayCertification . '"&gt;'

    虽然$BarangayCertification 的大小有上限,但我不建议将图像存储在您的数据库中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      • 2012-12-15
      • 2020-06-05
      相关资源
      最近更新 更多