2013-02-07 64 views
0

我目前正在爲我的最後一年大學項目製作網站,該項目需要照片上傳功能。當前用戶上傳照片時,照片存儲在遠程服務器的文件夾中。我需要圖像進入數據庫,所以我想知道是否有人對如何做到這一點以及在何處放置代碼以將下載的代碼發送到數據庫的任何建議,我也需要它在每個單獨用戶上傳圖像時都會顯示所有人都可以看到的圖像,而不是當前顯示的圖像,一次只顯示一個圖像,當頁面刷新時圖像消失。希望一切都有意義,任何幫助將不勝感激。謝謝。使用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:&nbsp; 
    <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"); ?> 
+1

由於在網站上存在太多其他問題:在數據庫中存儲文件在幾乎所有使用情況下都是一個壞主意。不要這樣做。您可能還需要更新代碼,以便插入您在網站中打開的各種安全漏洞,以及您正在使用的各種過時/棄用功能(ereg已經正式死機很長時間) –

+0

感謝迴應,我肯定會接受這個建議,並嘗試重新工作這段代碼。 – user1133181

回答

0

我強烈建議不要這樣做。而是將照片存儲在一個文件夾中,並從數據庫中引用它們的位置(即指向它們在文件系統上的位置的字符串)。

不過,如果你願意將其存儲在數據庫中,您需要:

  • 獲取該文件的內容上傳後
  • 確保內容不具有將任何字符與你的SQL衝突(簡單的解決方案是以某種方式進行編碼;或許的base64)
  • 執行你的SQL插入

再次 - 這是一個不好的的想法。不要這樣做 - 將其保存到文件系統。

此外,以下行:

move_uploaded_file($tmp_name, $n); 

沒有文件類型的任何檢查,或文件完整性,可以輕鬆地將一個外殼上傳到你的盒子。

0

成功上傳文件後,獲取上傳的圖像路徑。

$file_type='jpg'; //if you are using more than one type write a switch case 
$file_size = filesize($file); 
$fp = fopen($file,'r'); 
$content = fread($fp,$file_size); 
$content = addslashes($content); //content is a binary data of the image 
fclose($fp); 

將圖像保存到數據庫中。用你想要$ file_name,$ file_type,$ file_size和content的任何字段編寫插入查詢。我假設你能夠成功連接到數據庫。

mysql_query("INSERT INTO Images (id,file_name,file_type,file_size,content) 
    VALUES ('bird', 'jpg',340kb,'binarydata')");