2012-04-30 52 views
1

我讀過幾乎所有與此相關的主題。我不知道是什麼造成的。(PHP)圖像無法顯示,因爲它包含錯誤

這是用於示出圖像

<html> 
<body> 
<?php 

        mysql_connect('localhost','root','') or die("Unable to Connect: ".mysql_error()); 
        mysql_select_db("punjabi") or die("Unable to Select Database: ".mysql_error()); 

        $sql = "SELECT imagename, mimetype, imagedata 
          FROM images WHERE ID = 1"; 

        $result = @mysql_query($sql); 
        if(!$result) 
        { 
         die(mysql_error()); 
        } 

        $file = mysql_fetch_array($result); 

        $imagename = $file['imagename']; 
        $mimetype = $file['mimetype']; 
        $imagedata = $file['imagedata']; 

        header("content-type: $mimetype"); 

        echo($imagedata); 

?> 

代碼這是爲插入圖像

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Add Post</title> 
<style type="text/css"> 
    * 
    { 
     margin:0px; 
     padding:0px; 
    } 
</style> 

</head> 

<body bgcolor="#333333"> 

<?php if(isset($_POST['submit'])): 
     { 


      if (!is_uploaded_file($_FILES['uploadfile']['tmp_name'])) 
      { 
       die("$uploadfile is not an uploaded file!"); 
      } 

      $uploadfile = $_FILES['uploadfile']['tmp_name']; 
      $uploadname = $_FILES['uploadfile']['name']; 
      $uploadtype = $_FILES['uploadfile']['type']; 
      $uploaddesc = $_POST['desc']; 


      $tempfile = fopen($uploadfile,'rb'); 
      $filedata = fread($tempfile,filesize($uploadfile)); 
      $filedata = addslashes($filedata); 
      $sql = "INSERT INTO images SET 
      imagename = '$uploadname', 
      mimetype = '$uploadtype', 
      description = '$uploaddesc', 
      imagedata = '$filedata'"; 
      $ok = @mysql_query($sql); 
      if (!$ok) die("Database error storing file: " .mysql_error()); 

      $sql = "Select id from images where imagename = '$uploadname'"; 
      $result = @mysql_query($sql); 
      if(!$result) die(mysql_error()); 
      if(mysql_num_rows($result)!=1) die(mysql_error()); 

      $row = mysql_fetch_array($result); 
      $imageid = $row['id']; 

      $title = $_POST['title']; 
      $content = $_POST['content']; 

      $sql = "INSERT into posts SET 
        title = '$title', 
        content = '$content', 
        imageid = '$imageid'"; 
      if([email protected]_query($sql)) 
      { 
       die(mysql_error()); 
      } 


      header("Location:http://localhost/punjabi/adminhome.php"); 

     } 


?> 

<?php else: ?> 

    <div id="post"> 
     <form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="post" enctype="multipart/form-data" > 
      Title: <input type="text" name="title" /><br /><br /><br /> 
      Content:<br /> <textarea cols=100 rows="40" wrap="hard" name="content" /></textarea><br /><br /> 
      Image: <input type="file" name="uploadfile" /><br /><br /> 
      Image Desc: <input type="text" name="desc" /><br /><br /> 
      <input type="submit" value="Submit" name="submit" /> 
     </form> 
    </div> 

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

的代碼,這是表結構:

CREATE TABLE filestore (
-> ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 
-> FileName VARCHAR(255) NOT NULL, 
-> MimeType VARCHAR(50) NOT NULL, 
-> Description VARCHAR(255) NOT NULL, 
-> FileData MEDIUMBLOB 
->); 

和幫助,將不勝感激!

+2

爲什麼不使用參數化插入查詢而不是'addslashes()'?我認爲這個功能修改你的內容。 –

+0

當您直接在瀏覽器中打開生成圖像.php腳本時,您是否看到'警告:無法修改標頭信息 - 標頭已經由...發送? –

+0

小心直接在查詢中插入unsanitized'$ _POST'。 SQL注入是一個問題! – freshnode

回答

4

的問題是,頭已經發送當你這樣做:

<html> 
<body> 
<?php 

所以你不能設置像頭了:

header("content-type: $mimetype"); 

剛剛擺脫的HTML(一個圖像不是HTML /文本)在php標籤之前應該解決。

+0

謝謝老總:D我刪除了html和body標籤,它的工作原理:D – AntiSaby

+0

@AntiSaby不客氣。 – jeroen

+0

但我仍然感到困惑......我使用HTML文檔中的顯示圖像代碼......所以我應該如何解決它? – AntiSaby

相關問題