我目前正在爲我的最後一年大學項目製作網站,該項目需要照片上傳功能。當前用戶上傳照片時,照片存儲在遠程服務器的文件夾中。我需要圖像進入數據庫,所以我想知道是否有人對如何做到這一點以及在何處放置代碼以將下載的代碼發送到數據庫的任何建議,我也需要它在每個單獨用戶上傳圖像時都會顯示所有人都可以看到的圖像,而不是當前顯示的圖像,一次只顯示一個圖像,當頁面刷新時圖像消失。希望一切都有意義,任何幫助將不勝感激。謝謝。使用PHP將照片上傳到Mysql數據庫中
<?php include_once("home_start.php"); ?>
<h1>Upload your images here:</h1>
<div id="fileselect" style="border-bottom:thin #000000 solid; border- collapse:collapse">
<form id="frmSimple" action="home.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" id="filename" name="filename" size="10" /><br />
<input type="submit" id="submit" name="submit" value=" Upload " />
</form>
</div>
<div id="feedback">
<?php
// Determine whether a file was uploaded
if ($_FILES) {
// Put file properties into variables
$name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
$tmp_name = $_FILES['filename']['tmp_name'];
// Determine whether file is png, jpg or other
switch($_FILES['filename']['type']) {
case 'image/jpeg': $ext = "jpg"; break;
case 'image/png': $ext = "png"; break;
//default: ext = ''; break;
}
//validate against file type
// if $ext is empty string (therefore null or false) image is not a jpg or png
if($ext){
// validate against file size
if($size < 1000000){
// Create a safe name for the file and store in a safe location
$n = "$name"; // Could add .$ext to enforce file type
$n = ereg_replace("[^A-Za-z0-9.]","",$n); // Remove all except alphanumeric characters and
$n = strtolower($n); // Convert to lower case (platform independence)
$n = "uploaded_images/$n"; // Add folder to force safe location
move_uploaded_file($tmp_name, $n); // Move to the safe location and give it the safe
echo "<p>Uploaded image '$name' as '$n': </p>";
echo "<img src='$n' />";
}
else echo "<p>'$name' is too big - 50KB max (50000 bytes).</p>";
}
else echo "<p>'$name' is an invalid file - only jpg and png accepted.</p>";
}
else echo "<p>No image has been uploaded.</p>";
?>
</div>
<?php include_once("home_end.php"); ?>
由於在網站上存在太多其他問題:在數據庫中存儲文件在幾乎所有使用情況下都是一個壞主意。不要這樣做。您可能還需要更新代碼,以便插入您在網站中打開的各種安全漏洞,以及您正在使用的各種過時/棄用功能(ereg已經正式死機很長時間) –
感謝迴應,我肯定會接受這個建議,並嘗試重新工作這段代碼。 – user1133181