我通常不喜歡在帖子中添加第二個答案,所以我很抱歉提前。 如果這個幫助,我會刪除我的其他答案。
文件名:ImageHandler.php
<?php
class ImageHandler
{
public function __construct (mysqli $mysqli) {
$this->mysqli = $mysqli;
}
public function InsertImage ($filename) {
$absolute_filename = realpath ($filename);
if ($absolute_filename === true) {
if ($this->mysqli->query (sprintf ('INSERT INTO album (\'filename\') VALUES (\'%s\')', $absolute_filename))) {
return $this->mysqli->insert_id;
}
throw new Exception ('There was an error inserting the image into the database.');
}
throw new Exception ('Image "' . $filename . '" could not be found on the server.');
}
public function GetImage ($imageID) {
if ((is_integer ($imageID) == false) || ($imageID < 1)) {
throw new InvalidArgumentException ('The image ID must be an integer greater than 0');
}
$query = $mysqli->query (sprintf ('SELECT * FROM album WHERE id=%d', $imageId));
if ($query->num_rows > 0) {
$filename = null;
while ($row = $query->fetch_assoc()) {
$filename = $row [ 'filename' ];
}
$query->close();
if (isset ($filename)) {
$absolute_filename = realpath ($filename);
if ($absolute_filename === false) {
throw new Exception ('Image "' . $filename . '" could not be found on the server.');
}
return $absolute_filename;
}
throw new Exception ('There was an error returning the image filename from the database.');
}
$query->close();
throw new Exception ('There was an error getting an image filename from the database with an image id of [' . $imageId . '].');
}
public function PrintImage ($filename) {
$absolute_filename = realpath ($filename);
if ($absolute_filename === false) {
throw new Exception ('Image "' . $filename . '" could not be found on the server.');
}
header ('Content-Type: image/jpeg');
print file_get_contents ($absolute_filename);
}
private $mysqli;
}
?>
示例用法打印圖像到瀏覽器。
在開始之前,我們將對服務器目錄結構做一些假設。 ImageHandler.php存儲在image.php旁邊,一個包含所有圖像的受保護圖像文件夾位於父目錄的一個目錄中,並且無法通過Web瀏覽器訪問,位於images文件夾旁邊的是一個名爲logs的文件夾。
image.php
<?php
try {
if (isset ($_GET [ 'id' ]) == false) {
header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found');
return;
}
include 'ImageHandler.php';
include 'database_settings.php';
$mysqli = @new mysqli ($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
if ($mysqli->connect_errno > 0) {
throw new Exception ('There was an error connecting to the database.');
}
$image = new ImageHandler ($mysqli);
$filename = $image->GetImage ($_GET [ 'id' ]);
$image->PrintImage ($filename);
} catch (Exception $e) {
header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found');
file_put_contents ('../logs/error.log', $e->getMesage() . ' in file ' . $e->getFile() . ' on line ' . $e->getLine(). PHP_EOL, FILE_APPEND);
}
?>
存儲信息到我建議把這個文件的密碼保護的登錄背後的數據庫,圖像需要已經位於圖像的文件夾裏面
saveimage。 PHP
<?php
try {
if (isset ($_POST [ 'filename' ]) == false) {
throw new Exception ('Please complete this form to add an image to the database.');
}
$path = realpath ('../images');
if ($path === false) {
throw new Exception ('Images path does not exist.');
}
$path .= DIRECTORY_SEPARATOR . $_POST [ 'filename' ];
include 'ImageHandler.php';
include 'database_settings.php';
$mysqli = @new mysqli ($mysql_hostname, $mysql_username, $mysql_password, $mysql_database);
if ($mysqli->connect_errno > 0) {
throw new Exception ('There was an error connecting to the database.');
}
$image = new ImageHandler ($mysqli);
$id = $image->InsertImage ($path);
print 'Image saved as ID ' . $id . '<br><br><img src="image.php?id=' . $id .'"/>';
} catch (Exception $e) {
// form would go here and $e->getMessage() would be used to display messages to the browser
}
?>
你把圖片上傳到任意文件夾?你必須給不PHP文件 – user3386779
圖像文件的路徑,你居然stori ng數據庫中圖像的內容?或者ImageFile只是對某個名字的引用,比如something.jpg – MrTechie
似乎值得一提的是,您正在使用不推薦使用的庫,如[PHP手冊中所述](http://php.net/manual/en/intro.mysql .PHP);是否有一個原因,你沒有(至少)使用[MySQLi](http://php.net/manual/en/book.mysqli.php)版本? –