2012-08-29 58 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源代码时,您只需执行'

相关问题