如何傳圖檔到mysql資料庫 及把圖檔呈現在網頁中20點

請問各位高手 如題!

小弟上網看過這個網站http://www.hoyo.idv.tw/web-program/php/photo.htm

我照用他的語法但不能用

小弟是用mysql query browser

我想問如果要寫上傳圖檔的網頁 語法要怎樣寫 以及網頁中呈現影像的語法!

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    1.建立mysql資料庫及table

    >create database binary_data;

    >use binary_data;

    >CREATE TABLE binary_data (

    id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,

    description CHAR(50),

    bin_data LONGBLOB,

    filename CHAR(50),

    filesize CHAR(50),

    filetype CHAR(50)

    );

    2.建立store.php(這支php可供user上傳圖並存於table中)

    store.php

    <HTML>

    <HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>

    <BODY>

    <?php

    // code that will be executed if the form has been submitted:

    if ($submit) {

    // connect to the database

    // (you may have to adjust the hostname,username or password)

    MYSQL_CONNECT("localhost","root","password");

    mysql_select_db("binary_data");

    $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data)));

    $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".

    "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')");

    $id= mysql_insert_id();

    print "<p>This file has the following Database ID: <b>$id</b>";

    MYSQL_CLOSE();

    } else {

    // else show the form to submit new data:

    ?>

    <form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data">

    File Description:<br>

    <input type="text" name="form_description" size="40">

    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">

    <br>File to upload/store in database:<br>

    <input type="file" name="form_data" size="40">

    <p><input type="submit" name="submit" value="submit">

    </form>

    <?php

    }

    ?>

    </BODY>

    </HTML>

    3.建立getdata.php(這支php可擷取table中之二進位碼並用image輸出)

    getdata.php

    <?php

    if($id) {

    // you may have to modify login information for your database server:

    @MYSQL_CONNECT("localhost","root","password");

    @mysql_select_db("binary_data");

    $query = "select bin_data,filetype from binary_data where id=$id";

    $result = @MYSQL_QUERY($query);

    $data = @MYSQL_RESULT($result,0,"bin_data");

    $type = @MYSQL_RESULT($result,0,"filetype");

    Header( "Content-type: $type");

    echo $data;

    };

    ?>

    4.若要放在html中結合動態圖文請於html中使用image tag,用法如下

    <img src="getdata.php?id=1">

    Source(s): 自己手邊的code
    • Commenter avatarLogin to reply the answers
  • sony
    Lv 4
    1 decade ago

    1.表單注意

    <form name="form1" method="POST" action="bbs_process.php" enctype="multipart/form-data">

    2.

    圖片上傳到指定目錄並且縮小

    3.

    圖片展示只要將路徑內的圖片秀出即可(資料庫內上傳時需存入檔名)

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.