2014-04-08 39 views
0

我正在研究允許用戶插入文章的CMS,並且如果他想要他可以上傳通訊員文章的圖像,文章文本可以正常工作,問題在於圖像不是beeing顯示,即使我沒有上傳任何圖像的單詞「數組」出現在圖像所在的地方,我休耕與文章文本輸入相同的邏輯。圖像被存儲在類型爲「blob」的mysql中。有小費嗎?感謝來自數據庫的Php顯示圖像

PHP:

<?php 
session_start(); 
include_once('../includes/connection.php'); 
if(isset($_SESSION['logged_in'])) { 
    // display add page 
     if (isset($_POST['title'], $_POST['content'])) { 
     $title = $_POST['title']; 
     $content = $_POST['content']; 
     $image = $_FILES['image']['name']; 

      if (empty($title) || empty($content)) { 
       $error = 'Todos os campos têm de ser preenchidos!'; 
      } 

      else { 
       $query = $pdo->prepare('INSERT INTO articles(article_title, article_content, article_img, article_timestamp) VAlUES (?, ?, ?, ?)'); 

       $query->bindValue(1, $title); 
       $query->bindValue(2, $content); 
       $query->bindValue(3, $image); 
       $query->bindValue(4, time()); 

       $query->execute(); 

       header('Location:index.php'); 
      } 
     } 

} 
     else { 
     header('Location:index.php'); 
    } 
    ?> 

    <html> 
    <head> 
     <title>AdminPT</title> 
     <meta charset="UTF-8"> 
     <link rel ="stylesheet" href="../assets/style.css"/> 
    </head> 

     <body> 
      <div class="container"> 
       CMS 
       <br> 

       <h4 id ="add">Adicionar Artigo</h4> 

       <?php 
       if (isset($error)) { ?> 
        <small style="color:#aa0000"><?php echo $error; ?></small> 
       <?php } ?> 

       <form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data"> 
        <input id="title" type="text" name="title" placeholder="Título"/><br><br> 
        <textarea id="content" rows="15" placeholder="Conteúdo" name="content"></textarea> 
        <input type="file" name="image"/><br><br> 
        <input id="addBtn" type="submit" value="Adicionar Artigo"/> 
       </form> 
       <a id="back" href="../admin">&larr; Voltar</a> 
      </div> 
     </body> 
    </html> 

顯示內容(HTML):

<div id="news"> 
     <?php foreach ($articles as $article) { ?> 
     <div><h2><?php echo utf8_encode($article['article_title']); ?></h2><div id="newsImg"><?php echo $article['article_img']; ?></div><br><span id="date">Publicado 
       <?php 
        setlocale(LC_ALL, 'pt_BR.utf8', 'Portuguese_Brazil'); 
        //setlocale(LC_ALL, NULL); 
        date_default_timezone_set('Europe/Lisbon'); 

        $uppercaseMonth = ucfirst(gmstrftime('%B')); 
        echo utf8_encode(strftime('%a, '.$uppercaseMonth.' %d de %Y'/* - %H:%M'*/, $article['article_timestamp'])); 
       ?></span><p><?php echo utf8_encode($article['article_content']); ?><br><br><span id="print"><a onclick="javascript:window.print();" href="#">Imprimir</a></span><span id="link"><a href="#">Enviar link</a></p></div> 
       <?php } ?> 

    </div> 
+0

可能是您缺少圖像保存路徑<?php echo'uploads /'.$ article ['article_img']; ?> –

+0

圖像保存路徑?我怎麼知道它?對不起,我的無知 – Miguel

+0

你需要保存你的圖像的位置或在目錄也 –

回答

0

請改變這一行($圖像= $ _FILES [ '圖像'])到$圖像= $ _FILES [ '圖像'] [ 「名稱」];

+0

謝謝你是一個開始,如果現在它出現的圖像名稱(圖像.jpeg)而不是圖片本身 – Miguel

0

除了$ image = $ _FILES ['image'] [「name」];您需要在其之前添加其餘的相對/絕對路徑,例如

$image = "/uploadedimages/" . $_FILES['image']['name']; 
+0

謝謝,但我怎麼知道這條路? – Miguel

+0

實際的圖像沒有存儲在mysql中,只是任何圖像細節的數組。該圖像將存儲在特定的文件夾中,該文件夾的路徑將取決於您的CMS。 – Sanchez89

+0

我在php文件的同一文件夾下創建了一個imgs文件夾,但仍然出現「imgs/logoChi.png」而不是實際圖像 – Miguel

相關問題