我正在尋找存儲照片並從數據庫中檢索它,但我遇到了麻煩。我查看了其他帖子,但找不到解決方案。照片正在上傳到數據庫,但我似乎無法將其退出。另外,在url中,$ id = 0 但即使當我將$ id更改爲相應的數據庫編號時,它仍然只是加載一個空白頁面...我知道這是很多代碼,所以任何響應都會不勝感激!!存儲和檢索數據庫中的照片
CHOOSE_PHOTO.PHP
<form enctype="multipart/form-data" action="process_photo.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="image" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
PROCESS_PHOTO.PHP
MY_LOGIN_INFO_AND_DATABASE_CONNECTION
if ($_FILES) {
$image_types = array
("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
if (is_uploaded_file ($_FILES["image"]["tmp_name"])) {
$image = addslashes (fread
(fopen ($_FILES["image"]["tmp_name"], "r"),
filesize ($_FILES["image"]["tmp_name"])));
$file_name = $_FILES["image"]["name"];
$file_size = $_FILES["image"]["size"];
$file_type = $_FILES["image"]["type"];
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image_table "
. "(image_type, image, image_size, image_name, image_date) ";
sql.= "VALUES (";
$sql.= "'{$file_type}', '{$image}', '{$file_size}', "
. "'{$file_name}', NOW())";
@mysql_query ($sql, $conn);
exit();
}
}
}
$id = mysql_insert_id($conn);
mysql_close($conn);
header("Location: photo_preview.php?id=$id");
PHOTO_PREVIEW.PHP
MY_LOGIN_INFO_AND_DATABASE_CONNECTION
$result = mysql_query("SELECT * FROM image_table WHERE image_id= "
. mysql_real_escape_string($_GET["id"]), $conn);
if(!$result) die(mysql_error());
$row = mysql_fetch_assoc ($result);
if (mysql_num_rows($result) > 0)
{
$row = @mysql_fetch_assoc ($result);
$image_type = $row["image_type"];
$image = $row["image"];
Header ("Content-type: $image_type");
print $image;
}
?>