2014-10-10 103 views
0

我顯示的代碼是:我保證具有圖像和數據庫連接是正確的數據庫,但我不能顯示圖像

<?php 
include "file_constants.php"; 
// just so we know it is broken 
error_reporting(E_ALL); 
// some basic sanity checks 
if(isset($_GET['id']) && is_numeric($_GET['id'])) { 
    //connect to the db 
    $link = mysql_connect("$host", "$user", "$pass") 
    or die("Could not connect: " . mysql_error()); 

    // select our database 
    mysql_select_db("$db") or die(mysql_error()); 

    // get the image from the db 
    $sql = "SELECT image FROM test_image WHERE id=" .$_GET['id'] . ";"; 

    // the result of the query 
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); 

    // set the header for the image 
    header("Content-type: image/jpeg"); 

    echo mysql_result($result, 0); 
    // close the db link 
    mysql_close($link); 
} 
else { 
    echo 'Please use a real id number'; 
} 
?> 

我保證具有圖像和數據庫連接是正確的數據庫。 我可以從php上傳圖像到phpmyadmin(MYSQL)。 但是,我無法顯示圖像。(http:// /file_display.php?id=1) 有人可以幫助我在php中顯示圖像?非常感謝!

的文件插入代碼:

<html> 
<head><title>File Insert</title></head> 
<body> 
<h3>Please Choose a File and click Submit</h3> 

<form enctype="multipart/form-data" action= 
"<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 
<input name="userfile" type="file" /> 
<input type="submit" value="Submit" /> 
</form> 

<?php 

// check if a file was submitted 
if(!isset($_FILES['userfile'])) 
{ 
    echo '<p>Please select a file</p>'; 
} 
else 
{ 
    try { 
    $msg= upload(); //this will upload your image 
    echo $msg; //Message showing success or failure. 
    } 
    catch(Exception $e) { 
    echo $e->getMessage(); 
    echo 'Sorry, could not upload file'; 
    } 
} 

// the upload function 

function upload() { 
    include "file_constants.php"; 
    $maxsize = 10000000; //set to approx 10 MB 

    //check associated error code 
    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) { 

     //check whether file is uploaded with HTTP POST 
     if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {  

      //checks size of uploaded image on server side 
      if($_FILES['userfile']['size'] < $maxsize) { 

       //checks whether uploaded file is of image type 
       //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) { 
       $finfo = finfo_open(FILEINFO_MIME_TYPE); 
       if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {  

        // prepare the image for insertion 
        $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); 

        // put the image in the db... 
        // database connection 
        mysql_connect($host, $user, $pass) OR DIE (mysql_error()); 

        // select the db 
        mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error()); 

        // our sql query 
        $sql = "INSERT INTO test_image 
        (image, name) 
        VALUES 
        ('{$imgData}', '{$_FILES['userfile']['name']}');"; 

        // insert the image 
        mysql_query($sql) or die("Error in Query: " . mysql_error()); 
        $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>'; 
       } 
       else 
        $msg="<p>Uploaded file is not an image.</p>"; 
      } 
      else { 
       // if the file is not less than the maximum allowed, print an error 
       $msg='<div>File exceeds the Maximum File limit</div> 
       <div>Maximum File limit is '.$maxsize.' bytes</div> 
       <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. 
       ' bytes</div><hr />'; 
       } 
     } 
     else 
      $msg="File not uploaded successfully."; 

    } 
    else { 
     $msg= file_upload_error_message($_FILES['userfile']['error']); 
    } 
    return $msg; 
} 

// Function to return error message based on error code 

function file_upload_error_message($error_code) { 
    switch ($error_code) { 
     case UPLOAD_ERR_INI_SIZE: 
      return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
     case UPLOAD_ERR_FORM_SIZE: 
      return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
     case UPLOAD_ERR_PARTIAL: 
      return 'The uploaded file was only partially uploaded'; 
     case UPLOAD_ERR_NO_FILE: 
      return 'No file was uploaded'; 
     case UPLOAD_ERR_NO_TMP_DIR: 
      return 'Missing a temporary folder'; 
     case UPLOAD_ERR_CANT_WRITE: 
      return 'Failed to write file to disk'; 
     case UPLOAD_ERR_EXTENSION: 
      return 'File upload stopped by extension'; 
     default: 
      return 'Unknown upload error'; 
    } 
} 
?> 
</body> 
</html> 

的SQL是:

create table test_image (
id    int(10) not null AUTO_INCREMENT PRIMARY KEY, 
name   varchar(25) not null default '', 
image   blob  not null 
); 

該教程是http://vikasmahajan.wordpress.com/2010/07/07/inserting-and-displaying-images-in-mysql-using-php/

+0

不想使用''標籤嗎? – 2014-10-10 05:50:04

+0

噢,我想我想要標記 – RX93Gundam 2014-10-10 05:51:56

+1

你可以這樣做' 2014-10-10 05:54:01

回答

1

如果在數據庫中保存的只是形象的名字,這將顯示在您的HTML圖像

<?php 
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); 
    while($row = mysqli_fetch_array($result)) { 
     echo '<img src="www.yourdomain.com/your/directory/"'. $row["image "].'/>'; 
    } 
?> 

注意:如果您只有一行,則不需要使用while迴路

+0

我使用http://127.0.0.1/phpname.php?id=1,仍然無法顯示image ..... – RX93Gundam 2014-10-10 06:01:11

+0

是否打印了$ row數組?如果它包含圖像名稱,那麼您需要根據您的目錄結構調整路徑(如果您知道如何設置,則無需使用域名部分目錄路徑) – 2014-10-10 06:23:02

+0

我不知道你的意思,但是謝謝你的評論! – RX93Gundam 2014-10-10 06:33:43