2013-08-31 52 views
0

幾個月前我開始編寫我的第一個動態頁面......學習地面零點前端和後端的東西。在數據庫中插入圖片 - PHP - MySQL WITH用戶名

在這裏和那裏有幾個顛簸,所以這是多一個。

這是一個房地產頁面,人們可以添加列表和這樣的圖片。 我讀了一些關於數據庫內圖片問題的着名線索herethere。 我只是決定使用數據庫,因爲我需要從某處開始。

我用這個tutorial開始。幾小時後,瞧,我能夠在真實的服務器中插入圖片。很高興。

但隨後出現問題。圖片需要附加到用戶名(或電子郵件)。 當您嘗試插入內容時,該網站具有用戶名受保護的頁面。

這是插入圖片到數據庫的主要文件。請注意,我在代碼中添加了用戶名,但用戶名並未被插入,只是圖片。我需要知道誰在插入什麼。

有一個側面的問題,你可能會跳過:圖片需要附加到地址。當用戶選擇了圖片並按下了上傳按鈕時,我是否可以擁有一個輸入字段和地址,其中同一個按鈕將同時執行兩個功能? (如果我有兩個按鈕,用戶可能不會這樣做,這樣我們就可以同時獲取圖片和地址,並在後面輸入用戶名)。我會知道如何有兩個按鈕,兩個查詢。

非常感謝您的幫助。我真的需要它。

$query = sprintf(
     "insert into images (filename, mime_type, file_size, file_data, username) 
      values ('%s', '%s', %d, '%s','%s')", 
     mysql_real_escape_string($image['name']), 
     mysql_real_escape_string($info['mime']), 
     $image['size'], 
     mysql_real_escape_string(
      file_get_contents($image['tmp_name'])), 
    mysql_real_escape_string(
      file_get_contents($_SESSION['user']) 
     ) 
    ); 

這裏是完整的代碼。

<?php 
//error_reporting(E_ALL); 
//ini_set("display_errors", 1); 
require("common.php"); 
if(empty($_SESSION['user']))  
    { 
     header("Location: ../index.php"); 
     die("Redirecting to .../index.php"); 
    } 

?> 

以上代碼的第一部分只是檢查用戶是否已經登錄。然後是代碼。

<?php 
    require_once('globalsConfigPic1.php'); 

    function assertValidUpload($code) 
    { 
     if ($code == UPLOAD_ERR_OK) { 
      return; 
     } 

     switch ($code) { 
      case UPLOAD_ERR_INI_SIZE: 
      case UPLOAD_ERR_FORM_SIZE: 
       $msg = 'Image is too large'; 
       break; 

      case UPLOAD_ERR_PARTIAL: 
       $msg = 'Image was only partially uploaded'; 
       break; 

      case UPLOAD_ERR_NO_FILE: 
       $msg = 'No image was uploaded'; 
       break; 

      case UPLOAD_ERR_NO_TMP_DIR: 
       $msg = 'Upload folder not found'; 
       break; 

      case UPLOAD_ERR_CANT_WRITE: 
       $msg = 'Unable to write uploaded file'; 
       break; 

      case UPLOAD_ERR_EXTENSION: 
       $msg = 'Upload failed due to extension'; 
       break; 

      default: 
       $msg = 'Unknown error'; 
     } 

     throw new Exception($msg); 
    } 

    $errors = array(); 

    try { 
     if (!array_key_exists('image', $_FILES)) { 
      throw new Exception('Image not found in uploaded data'); 
     } 

     $image = $_FILES['image']; 

     // ensure the file was successfully uploaded 
     assertValidUpload($image['error']); 

     if (!is_uploaded_file($image['tmp_name'])) { 
      throw new Exception('File is not an uploaded file'); 
     } 

     $info = getImageSize($image['tmp_name']); 

     if (!$info) { 
      throw new Exception('File is not an image'); 
     } 
    } 
    catch (Exception $ex) { 
     $errors[] = $ex->getMessage(); 
    } 

    if (count($errors) == 0) { 
     // no errors, so insert the image 

     $query = sprintf(
     "insert into images (filename, mime_type, file_size, file_data, username) 
      values ('%s', '%s', %d, '%s','%s')", 
     mysql_real_escape_string($image['name']), 
     mysql_real_escape_string($info['mime']), 
     $image['size'], 
     mysql_real_escape_string(
      file_get_contents($image['tmp_name'])), 

     mysql_real_escape_string(
      file_get_contents($_SESSION['user']) 
     ) 
    ); 

     mysql_query($query, $db); 

     $id = (int) mysql_insert_id($db); 

     // finally, redirect the user to view the new image 
     header('Location: view.php?id=' . $id); 
     exit; 
    } 
?> 
<html> 
    <head> 
     <title>Error</title> 
    </head> 
    <body> 
     <div> 
      <p> 
       The following errors occurred: 
      </p> 

      <ul> 
       <?php foreach ($errors as $error) { ?> 
        <li> 
         <?php echo htmlSpecialChars($error) ?> 
        </li> 
       <?php } ?> 
      </ul> 

      <p> 
       <a href="upload.php">Try again</a> 
      </p> 
     </div> 
    </body> 
</html> 
+0

-1爲了在數據庫中存儲圖像的想法。 –

回答

-1

是的! 下面是第一部分中的部分解決方案:

$query = sprintf(
      "insert into images (filename, mime_type, file_size, file_data, username) 
       values ('%s', '%s', %d, '%s','%s')", 
      mysql_real_escape_string($image['name']), 
      mysql_real_escape_string($info['mime']), 
      $image['size'], 
      mysql_real_escape_string(
       file_get_contents($image['tmp_name'])), 

      mysql_real_escape_string (
       htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8') 
      ) 
     ); 

現在該圖片努力與地址連接。