您可以存儲所有信息的一種方式,並且使用文件系統的圖像是通過組合這兩個!
如果您有一個表格,其中包含圖像列表,每個圖像都帶有圖像文件名,上傳時間和標題。
然後,你可以寫一個簡單的類象下面這樣來管理每一個圖像:如果你有一個像這樣在你的數據庫中的表
class Image {
private $info;
public function __construct(int $id, $mysqli) {
$info = $mysqli->query("SELECT * FROM `images` WHERE `id`=" . $id);
$this->info = array();
$this->info["id"] = $info["id"];
$this->info["src"] = "/my/path/" . $info["name"] . "." . $info["type"];
$this->info["upload"] = strtotime($info["upload"]);
$this->info["tags"] = json_decode($info["tags"], true);
}
public function Id() { return $this->info["id"]; }
public function Src() { return $this->info["src"]; }
public function Upload() { return $this->info["upload"]; }
public function Tags() { return $this->info["tags"]; }
}
這個簡單的類將工作:
+----------------------------------+------------+-----------+-----------------+-------------+
| id INT NOT NULL AUTO_INCREMENT | name TEXT | type TEXT | upload DATETIME | tags TEXT |
+----------------------------------+------------+-----------+-----------------+-------------+
所以現在,如果您知道要獲取的圖像的ID,可以使用以下代碼得到它:
$id = 57;
$img = new Image($id, $connection);
現在我們可以獲得有關圖像的信息!
echo $img->Id() // output: 57
echo $img->Src() // output: /my/path/image.png
echo $img->Upload() // output: 1375858084
Tags()
函數將返回一個數組,JSON從數據庫的列中解碼。
我覺得每個人都有同樣的想法在這裏,我就是這樣的最好的,因爲: 1.我真的不使用多... 2.它確實看起來像「classiest」的方式類做事情(雙關語) 3.感覺很好因爲還在使用文件系統 –