[英]Upload photo to database (to server not localhost)
我有一個學校項目。 我需要做一個花店和一種上傳新產品(花)的方法。 所以我制作了一個頁面,在其中添加了一個新產品,除了圖像之外它都可以工作。 我只是不知道如何將照片發送到數據庫。 我使用學校的域,互聯網上的所有示例都顯示了如何對本地主機執行此操作。 我不知道如何進一步解釋。
產品/花有名稱、類別、價格、描述。
這是我的“添加產品”頁面的樣子(不包括添加照片):
<html lang="en">
<?php include '../head.php' ?>
<?php
include '../databasecon.php';
?>
<body>
<!--================Top Header Area =================-->
<header class="shop_header_area carousel_menu_area">
<!--================Categories Product Area =================-->
<section class="categories_product_main p_80">
<div class="container" style="max-width: unset;">
<div class="col-lg-12" style="padding: 0; text-align:center;">
<h1 style="margin:0; padding-bottom: 40px; color: #09366C; font-weight: bold; text-align:left;"> Bloem toevoegen </h1>
</div>
<div class="categories_main_inner">
<div class="row row_disable">
<div class="col-lg-12">
</div>
<?php include 'menubeheer.php' ?>
<div class="float-left col-lg-9">
<?php
$conn = Opencon();
$QUERY = "SELECT * FROM producten";
if (!empty($_POST)) {
$naam = htmlspecialchars($_POST['product_naam']);
$categorie = htmlspecialchars($_POST['product_categorie']);
$prijs = htmlspecialchars($_POST['product_prijs']);
$omschrijving = htmlspecialchars($_POST['product_omschrijving']);
$insert = "INSERT INTO producten (product_naam,product_categorie,product_prijs,product_omschrijving)
VALUES('$naam','$categorie','$prijs','$omschrijving')";
}
?>
<form action="bloemtoevoegen.php" method="POST">
<div class="col-lg-2" style="float: left; margin-top: 10px;"> Product naam </div>
<div class="col-lg-10" style="float: left; margin-top: 10px;">
<input type="text" name="product_naam" style="width: 35%;" required><br>
</div>
<div class="col-lg-2" style="float:left; margin-top: 10px;"> Categorie </div>
<div class="col-lg-10" style="float: left; margin-top: 10px;">
<select name="product_categorie">
<option value="bloem">Losse bloem</option>
<option value="boeket">Boeket</option>
</select>
</div>
<div class="col-lg-2" style="float: left; margin-top: 10px;"> Prijs </div>
<div class="col-lg-10" style="float: left; margin-top: 10px;">
<input type="text" name="product_prijs" style="width: 35%;" required><br>
</div>
<div class="col-lg-2" style="float: left; margin-top: 10px;"> Omschrijving </div>
<div class="col-lg-10" style="float: left; margin-top: 10px;">
<input type="text" name="product_omschrijving" style="width: 50%; height:100%;" required><br>
</div>
<div class="col-lg-2" style="float: left; margin-top: 10px;"> Foto </div>
<div class="col-lg-10" style="float: left; margin-top: 10px;">
<input type="file" name="product_foto">
</div>
<div class="col-lg-12">
<b>Liefst een foto met afmetingen van 300 bij 200!</b>
</div>
<!-- <div class="col-lg-10" style="float: left; margin-top: 10px;">
<input type="text" name="product_fotos" style="width: 35%;" required><br>
</div> -->
<br>
<input type="submit" class="add_cart_btn" style="cursor: pointer; margin-top:30px;" value="Opslaan" name="submit">
</form>
</div>
</div>
</div>
</div>
</section>
<?php include '../script.php' ?>
</body>
</html>
第一步是將表單標簽上的 enctype 屬性設置為 multipart/form-data。 您可以在此處閱讀有關其作用的更多信息:
<form action="bloemtoevoegen.php" method="POST" enctype="multipart/form-data">
提交表單后,我們將文件保存到一個目錄並將路徑存儲在我們的數據庫中:
if (!empty($_POST) && !empty($_FILES["product_foto"]["name"])) {
$naam = htmlspecialchars($_POST['product_naam']);
$categorie = htmlspecialchars($_POST['product_categorie']);
$prijs = htmlspecialchars($_POST['product_prijs']);
$omschrijving = htmlspecialchars($_POST['product_omschrijving']);
$target_directory = getcwd() . "/uploads/"; // The location to store the files
$file_name = basename($_FILES["product_foto"]["name"]); // The uploaded file name
$target_path = $target_directory . $file_name; // The path to our file
if ( move_uploaded_file($_FILES["product_foto"]["tmp_name"], $target_path) ) {
// We have added "product_foto" to the INSERT INTO and added $file_name to the VALUES
$insert = "INSERT INTO producten (product_naam,product_categorie,product_prijs,product_omschrijving,product_foto)
VALUES('$naam','$categorie','$prijs','$omschrijving','$file_name')";
} else {
die("Unable to save file to disk");
}
}
注意事項:
您需要創建一個名為uploads
的目錄。 它將位於您添加產品文件所在的同一目錄中。
我假設您在INSERT INTO
語句中的數據庫表中有一個名為product_foto
的字段。 您可能需要更改它以匹配您的實際字段名稱
您現在應該將圖像文件存儲在您的數據庫中,並且該文件應該存在於您的目錄中。
如果您希望存儲路徑,請更改為以下行:
$insert = "INSERT INTO producten (product_naam,product_categorie,product_prijs,product_omschrijving,product_foto)
VALUES('$naam','$categorie','$prijs','$omschrijving','$target_path')";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.