2012-08-29 91 views
-2

我正在通過一本關於二進制數據的書的章節。我想要做的是自動顯示一個人的圖片,因爲我的數據庫處理配置文件。顯示個人資料照片而不是指向該照片的鏈接

到目前爲止,我的解決方案工作和照片是拼圖的最後一塊。

本書讓您進入文件名鏈接輸出到屏幕的階段,點擊此鏈接可顯示圖片。

我想要做的而不是這個,就是像Facebook上那樣自動顯示圖片。在那裏你不會看到一個鏈接到你的個人資料圖片,但實際圖片本身。

代碼如下所示:

的index.php(控制器)

<?php 
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/magicquotes.inc.php'; 

if (isset($_POST['action']) and $_POST['action'] == 'upload') 
{ 
    // Bail out if the file isn't really an upload 
    if (!is_uploaded_file($_FILES['upload']['tmp_name'])) 
    { 
    $error = 'There was no file uploaded!'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
    } 
    $uploadfile = $_FILES['upload']['tmp_name']; 
    $uploadname = $_FILES['upload']['name']; 
    $uploadtype = $_FILES['upload']['type']; 
    $uploaddesc = $_POST['desc']; 
    $uploaddata = file_get_contents($uploadfile); 

    include 'db.inc.php'; 

    try 
    { 
    $sql = 'INSERT INTO filestore SET 
     filename = :filename, 
     mimetype = :mimetype, 
     description = :description, 
     filedata = :filedata'; 
    $s = $pdo->prepare($sql); 
    $s->bindValue(':filename', $uploadname); 
    $s->bindValue(':mimetype', $uploadtype); 
    $s->bindValue(':description', $uploaddesc); 
    $s->bindValue(':filedata', $uploaddata); 
    $s->execute(); 
    } 
    catch (PDOException $e) 
    { 
    $error = 'Database error storing file!'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
    } 

    header('Location: .'); 
    exit(); 
} 

if (isset($_GET['action']) and 
    ($_GET['action'] == 'view' or $_GET['action'] == 'download') and 
    isset($_GET['id'])) 
{ 
    include 'db.inc.php'; 

    try 
    { 
    $sql = 'SELECT filename, mimetype, filedata 
     FROM filestore 
     WHERE id = :id'; 
    $s = $pdo->prepare($sql); 
    $s->bindValue(':id', $_GET['id']); 
    $s->execute(); 
    } 
    catch (PDOException $e) 
    { 
    $error = 'Database error fetching requested file.'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
    } 

    $file = $s->fetch(); 
    if (!$file) 
    { 
    $error = 'File with specified ID not found in the database!'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
    } 

    $filename = $file['filename']; 
    $mimetype = $file['mimetype']; 
    $filedata = $file['filedata']; 
    $disposition = 'inline'; 

    if ($_GET['action'] == 'download') 
    { 
    $mimetype = 'application/octet-stream'; 
    $disposition = 'attachment'; 
    } 

    // Content-type must come before Content-disposition 
    header('Content-length: ' . strlen($filedata)); 
    header("Content-type: $mimetype"); 
    header("Content-disposition: $disposition; filename=$filename"); 

    echo $filedata; 
    exit(); 
} 

if (isset($_POST['action']) and $_POST['action'] == 'delete' and 
    isset($_POST['id'])) 
{ 
    include 'db.inc.php'; 

    try 
    { 
    $sql = 'DELETE FROM filestore 
     WHERE id = :id'; 
    $s = $pdo->prepare($sql); 
    $s->bindValue(':id', $_POST['id']); 
    $s->execute(); 
    } 
    catch (PDOException $e) 
    { 
    $error = 'Database error deleting requested file.'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
    } 

    header('Location: .'); 
    exit(); 
} 

include 'db.inc.php'; 

try 
{ 
    $result = $pdo->query(
     'SELECT id, filename, mimetype, description 
     FROM filestore'); 
} 
catch (PDOException $e) 
{ 
    $error = 'Database error fetching stored files.'; 
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php'; 
    exit(); 
} 

$files = array(); 
foreach ($result as $row) 
{ 
    $files[] = array(
     'id' => $row['id'], 
     'filename' => $row['filename'], 
     'mimetype' => $row['mimetype'], 
     'description' => $row['description']); 
} 

include 'files.html.php'; 

PHOTO.HTML(查看)

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.inc.php'; ?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>PHP/MySQL File Repository</title> 
    </head> 
    <body> 
    <h1>PHP/MySQL File Repository</h1> 

    <form action="" method="post" enctype="multipart/form-data"> 
     <div> 
     <label for="upload">Upload File: 
     <input type="file" id="upload" name="upload"></label> 
     </div> 
     <div> 
     <label for="desc">File Description: 
     <input type="text" id="desc" name="desc" maxlength="255"></label> 
     </div> 
     <div> 
     <input type="hidden" name="action" value="upload"> 
     <input type="submit" value="Upload"> 
     </div> 
    </form> 

    <?php if (count($files) > 0): ?> 

    <p>The following files are stored in the database:</p> 

    <table> 
     <thead> 
     <tr> 
      <th>Filename</th> 
      <th>Type</th> 
      <th>Description</th> 
     </tr> 
     </thead> 
     <tbody> 
     <?php foreach($files as $f): ?> 
     <tr> 
      <td> 
      <a href="?action=view&amp;id=<?php htmlout($f['id']); ?>" 
       ><?php htmlout($f['filename']); ?></a> 
      </td> 
      <td><?php htmlout($f['mimetype']); ?></td> 
      <td><?php htmlout($f['description']); ?></td> 
      <td><?php htmlout($f['filedata']); ?></td> 

      <td> 
      <form action="" method="get"> 
       <div> 
       <input type="hidden" name="action" value="download"/> 
       <input type="hidden" name="id" value="<?php htmlout($f['id']); ?>"/> 
       <input type="submit" value="Download"/> 
       </div> 
      </form> 
      </td> 
      <td> 
      <form action="" method="post"> 
       <div> 
       <input type="hidden" name="action" value="delete"/> 
       <input type="hidden" name="id" value="<?php htmlout($f['id']); ?>"/> 
       <input type="submit" value="Delete"/> 
       </div> 
      </form> 
      </td> 
     </tr> 
     <?php endforeach; ?> 
     </tbody> 
    </table> 

    <?php endif; ?> 
    </body> 
</html> 

所有幫助表示讚賞。

中提取當前PHOTO.HTML

<?php foreach($files as $f): ?> 
    <tr> 
     <td> 
     <a href="?action=view&amp;id=<?php htmlout($f['id']); ?>" 
      ><?php htmlout($f['filename']); ?></a> 

      <!-- attempt to output image not path --> 
      <img src="<?php echo htmlout($f['filename']); ?>" /> 
     </td> 

Helper功能

<?php 

function html($text) 
{ 
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8'); 
} 

function htmlout($text) 
{ 
    echo html($text); 
} 

?> 
+0

''標籤不夠? – Matt

+0

對於這個問題的道歉顯示缺乏思想,但我被困住了,不知道從哪裏開始。 – Johnny

回答

2

把它包在<img>標籤。這會將其輸出爲圖像。

<img src="<?php echo $image; ?>">

+1

-1:從什麼時候開始有''? –

+0

@MarcB - 這是一個錯字。固定。 –

+0

不,這不起作用。 – Johnny

1

在大多數情況下,你要通過地方保持上的目錄結構,它是通過HTTP,只是存儲在數據庫中的鏈接,圖像位置可公開獲得的圖像文件得到更好的服務。

因此,例如,當用戶上載圖像時,將其放置在某個用戶圖像目錄的Web目錄中,然後將該圖像的路徑或URL存儲在數據庫中的varchar字段中。

這爲您提供了減小數據庫大小的好處,使您能夠更快地查詢數據庫中的圖片信息,提高瀏覽器對圖像的緩存速度,並允許您將靜態文件保存在一個位置(可能位於CDN再次在最終用戶的瀏覽器中獲得更好的性能)。

+0

任何這樣的例子可以實現嗎? – Johnny

+1

在StackOverflow或Internet上有大量的例子顯示如何將$ _FILES的內容寫入到本地目錄然後你可以獲取表示文件寫入位置的字符串值並將其放入數據庫中,假設'$ filepath = '/ images/profiles /'。 $ uploadname',它可能對應於'/ var/www/images/profiles/UPLOADNAME'磁盤上的物理位置。您將'$ filepath'保存到數據庫,並在打印出HTML源代碼時,您只需執行'

相關問題