簡體   English   中英

使用php將照片上傳到Mysql數據庫

[英]Photo uploading using php into a Mysql database

我目前正在為我的大學最后一年的項目建立一個網站,該網站需要照片上傳功能。 當前,當用戶上傳照片時,照片存儲在遠程服務器的文件夾中。 我需要將圖像保存到數據庫中,因此我想知道是否有人在以下代碼中對如何執行此操作以及在何處放置代碼以將上載的內容發送到數據庫方面有任何建議,我也需要它當每個用戶上載圖像時,這些圖像都會全部顯示給所有人查看,而不是像現在這樣顯示,一次只顯示一幅圖像,刷新頁面后,圖像就會消失。 希望一切都有意義,任何幫助將不勝感激。 謝謝。

<?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"); ?>

我強烈建議您反對。 取而代之的是,將照片存儲在文件夾中,並從數據庫中引用它們的位置(即指向它們在文件系統上位置的字符串)。

但是,如果您傾向於將其存儲在數據庫中,則需要:

  • 上傳后獲取文件內容
  • 確保內容中沒有與SQL沖突的字符(簡單的解決方案是以某種方式對其進行編碼;也許是base64?)
  • 執行您的SQL插入

再次-這是一個主意。 不要這樣做-將其保存到文件系統中。

另外,以下行:

move_uploaded_file($tmp_name, $n);

無需對文件類型或文件完整性進行任何檢查,就可以輕松地將Shell上傳到您的盒子中。

成功上傳文件后,一旦獲得上傳的圖像路徑。

    $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和內容。 我假設您能夠成功連接到數據庫。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM