問(wèn)題描述
如何在 MySQL 數(shù)據(jù)庫(kù)中存儲(chǔ)和顯示圖像.到目前為止,我只編寫(xiě)了從用戶那里獲取圖像并將它們存儲(chǔ)在一個(gè)文件夾中的代碼,我編寫(xiě)到現(xiàn)在的代碼是:HTML 文件
How can i store and display the images in a MySQL database. Till now i have only written the code to get the images from the user and store them in a folder, the code that i wrote till now is: HTML FILE
<input type="file" name="imageUpload" id="imageUpload">
PHP 文件
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";}
推薦答案
我找到了答案,對(duì)于那些在這里尋找同樣事情的人,我就是這樣做的.您不應(yīng)該考慮將圖像上傳到數(shù)據(jù)庫(kù),而是可以將上傳文件的名稱存儲(chǔ)在數(shù)據(jù)庫(kù)中,然后檢索文件名并在您想要顯示圖像的任何地方使用它.
I found the answer, For those who are looking for the same thing here is how I did it. You should not consider uploading images to the database instead you can store the name of the uploaded file in your database and then retrieve the file name and use it where ever you want to display the image.
HTML 代碼
<input type="file" name="imageUpload" id="imageUpload">
PHP 代碼
if(isset($_POST['submit'])) {
//Process the image that is uploaded by the user
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["imageUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
$image=basename( $_FILES["imageUpload"]["name"],".jpg"); // used to store the filename in a variable
//storind the data in your database
$query= "INSERT INTO items VALUES ('$id','$title','$description','$price','$value','$contact','$image')";
mysql_query($query);
require('heading.php');
echo "Your add has been submited, you will be redirected to your account page in 3 seconds....";
header( "Refresh:3; url=account.php", true, 303);
}
顯示圖像的代碼
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><img src='uploads/$row[6].jpg' height='150px' width='300px'></td>";
echo "</tr>
";
}
這篇關(guān)于如何使用php將圖像存儲(chǔ)在mysql數(shù)據(jù)庫(kù)中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!