-3

我使用此代码上传图片。我从stackoverflow得到了这段代码。我仍然无法上传图片。我更改了表设置2中的db连接设置。我制作了表,但我不确定我创建的表的属性是否正确。

<html>
<head><title>Store Some Binary File to a DB</title></head>
<body>

<?php
if ($submit) {
    $conn = mysql_connect("localhost", "your_username", "your_password") 
        or die ("Error connecting to database.");

    mysql_select_db("your_database", $conn) 
        or die ("error connecting to database.");

    $data = addslashes(file_get_contents($_FILES['form_data']['tmp_name'])


    $result = mysql_query ("INSERT INTO your_table ".
         "(description,bin_data,filename,filesize,filetype) ".
         "VALUES ('$form_description','$data','$form_data_name',".
         "'$form_data_size','$form_data_type')",
         $conn);

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

    mysql_close();

} else {
?>

    <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>

另外,帮助我从数据库中检索图像并将其显示在页面上。

你认为这现在行得通吗?

4

3 回答 3

1

The problem with storing images on the filesystem comes down to database replication. You would also need filesystem replication which can sometimes be harder than database replication especially when some servers may be remote.

None of your $form_ variables are set (unless theres code I cant see or you 'dangerously' have register globals on). Also you will want to addslashes (or mysql_real_escape_string) on any queries that accept REQUEST values, else your script is prone to SQL injection.

You want to:

$form_description = isset($_POST['form_description']) ? $_POST['form_description'] : false;

The other variables are not even being posted. Do you know any HTML and PHP? this is easy to resolve but I am not writing this for you!

What we need is some feedback what the script produces.. you can get the image back by doing:

$result = mysql_query ("SELECT `bin_data` FROM `your_table` WHERE `id` = ".$id, $conn);

Then do for example (replacing the content type with the relevant type determined from the files array):

$result_data = mysql_fetch_array($result, MYSQL_ASSOC);
header('Content-Type: image/jpg');
echo $result_data['bin_data'];
exit;

You need to do a bit more work to that script, and that requires knowledge of HTML, PHP and MySQL.

于 2008-12-12T12:28:23.380 回答
1

我不建议将您的二进制图像文件直接存储在数据库中。相反,将其存储在磁盘上并将文件指针(文件目录+文件名)存储在数据库中。

于 2008-12-11T17:33:26.847 回答
1

http://www.php.net/manual/en/features.file-upload.php

而不是使用 $form_data 作为上传文件的文件名,尝试$_FILES['form_data']['tmp_name']

你也可以替换相当笨拙的

fread(fopen($form_data, "r"), filesize($form_data))

file_get_contents($_FILES['form_data']['tmp_name'])
于 2008-12-11T16:27:38.167 回答