对于一个项目,我需要使用PHP为后端构建某种拍卖平台。我目前正在尝试使用PHP上传图像,然后将url存储在我的sql数据库的“Item”表中。我按照tutorial进行了图片上传,但是,现在我不确定如何将它连接到我的数据库。我的图片上传代码如下: 服务器>图像> upload.php的:
// Tutorial: https://www.w3schools.com/php/php_file_upload.asp
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size - if more than 500MB, display error
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats (JPG, JPEG, PNG, and GIF)
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
如上所述,我想检索图像网址并将其存储在“项目”表中。我通过使用this tutorial构建API来连接数据库。所以基本上我的“Item”模型如下所示: server&gt; &model; Item.php
<?php
include_once '../sql_functions.php';
class Item
{
// database connection and table name
private $conn;
// object properties
public $ID;
public $Name;
public $Description;
public $AuctionStart;
public $AuctionEnd;
public $AuctionFinished;
public $StartingPrice;
public $ReservePrice;
public $FinalPrice;
public $PhotoURL;
public $SellerID;
// constructor with $db as database connection
public function __construct($db)
{
$this->conn = $db;
}
// read products
function read()
{
return p_Item_sel_all($this->conn);
}
// search products
function search($search)
{
return p_Item_search($this->conn,$search);
}
function create()
{
// TODO: Check that start date is in the future, and before the end date
// check reserver price is positive
// sanitize
$this->Name = htmlspecialchars(strip_tags($this->Name));
$this->Description = htmlspecialchars(strip_tags($this->Description));
$this->AuctionStart = htmlspecialchars(strip_tags($this->AuctionStart));
$this->AuctionEnd = htmlspecialchars(strip_tags($this->AuctionEnd));
$this->StartingPrice = htmlspecialchars(strip_tags($this->StartingPrice));
$this->ReservePrice = htmlspecialchars(strip_tags($this->ReservePrice));
$this->PhotoURL = htmlspecialchars(strip_tags($this->PhotoURL));
$this->SellerID = htmlspecialchars(strip_tags($this->SellerID));
// $this->created=htmlspecialchars(strip_tags($this->created));
if (p_Item_ins($this->conn, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->StartingPrice, $this->ReservePrice, $this->PhotoURL, $this->SellerID)) {
return true;
};
return false;
}
// used when filling up the update product form
function readOne()
{
$stmt = p_Item_sel_id($this->conn, $this->ID);
// get retrieved row
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// set values to object properties
$this->ID = $row['ID '];
$this->Name = $row['Name '];
$this->Description = $row['Description '];
$this->AuctionStart = $row['AuctionStart '];
$this->AuctionEnd = $row['AuctionEnd '];
$this->AuctionFinished = $row['AuctionFinished '];
$this->StartingPrice = $row['StartingPrice '];
$this->ReservePrice = $row['ReservePrice '];
$this->FinalPrice = $row['FinalPrice '];
$this->PhotoURL = $row['PhotoURL '];
$this->SellerID = $row['SellerID '];
}
function increment_views()
{
return p_Item_incr_views($this->conn, $this->ID);
}
function update(){
// execute the query
if (p_Item_upd($this->conn, $this->ID, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->AuctionFinished, $this->StartingPrice, $this->ReservePrice, $this->FinalPrice, $this->PhotoURL, $this->SellerID)) {
return true;
}
return false;
}
// delete the Item
function delete(){
// execute query
if(p_Item_del_id($this->conn, $this->ID)){
return true;
}
return false;
}
}
函数(创建,删除,read_one,读取,搜索,更新)都在不同的文件中。 SQL查询存储在sproc文件中。如果需要,我当然也可以在这里包含代码。
这可能是从上传到数据库的一个非常简单的连接,但由于我是一个严格的项目截止日期的PHP新手,我非常感谢任何帮助!
答案 0 :(得分:1)
在课堂上,你错过了&#34; setter&#34;
您需要将数据库连接器实例化为适当的对象...
public function setDbObject($domain, $user, $pw, $db) {
$conn = mysqli_connect("$domain", "$user", "$pw", "$db");
if (!$conn) {
return false();
}
else {
return $conn;
}
}
这将返回你的连接&#34;对象&#34;然后,您可以使用它来运行查询。
希望有所帮助。
答案 1 :(得分:0)
我的建议是为您的文件(或图像)定义一些存储路径,并定义该文件夹的URL 例如
define('STORAGE_DIR',dirname(__FILLE__).'/../storage/');
define('STORAGE_url','http://example.com/storage/');
然后,为您的文件创建一些随机名称,并将名称保存在数据库中。如果你这样做,你就会知道你的任何文件的确切位置和网址。