2011-01-14 37 views
1

我正在創建一個用戶可以上傳約6張圖片的調查。將這些文件存儲在文件系統中的最佳方式是什麼?用戶可以做多的調查(從而跟蹤它們隨時間的改善)......這樣就意味着我不能只是他們的名字順序...上傳命名約定

這是一個CI/PHP應用程序。 :)

或者爲了知識的緣故,你會怎麼做?

+2

爲何不按順序? `survey1_image6.jpg`? – 2011-01-14 19:50:11

+0

所以你需要跟蹤,用戶,調查和圖像? – 2011-01-14 19:52:11

+0

@Phill,我只需要一個簡單的方法來跟蹤圖像和用戶,但我不能只是將它們命名爲`user12_image3` ... – 2011-01-14 19:56:42

回答

0

考慮順序編號:

user123_survey1_image6.jpg 

用戶爲123,而這是用戶的第六圖像,或甚至更容易的ID:

user123_survey56_image899.jpg 

其中用戶123的第六圖像恰好有ID 899

1

哇,這是有點開放式的。我想我可能會結構的目錄爲每個用戶,然後將其命名基於他們上傳的日期時間的影像。所以它看起來像這樣:

/+ 
| 
+--+ Kyle 
    | 
    +-- 20110113114903.png 
    | 
    +-- 20110113115122.png 
    | 
    +-- 20110114010304.png 
+--+ Kevin 
    | 
    +-- 20101114123456.png 
    | 
    +-- 20110102023648.png 
    | 
    +-- ... 

但是,這可能不像您喜歡的那樣具有可擴展性。您可能希望更深入地設置目錄,以便每個人都可以獲取每個日期的目錄,並在其中顯示圖像。

1

映射這一切都在一個數據庫中,你讓他們存儲在文件系統上,其中將在搜索調查和用戶的最佳結果。

// Pseudo Database 
// user table 
user_id (pk) 
user_name 
user_filesystem_dir 
... 

// survey table 
survey_id (pk) 
survey_name 
survey_filesystem_dir 
... 

// Image table 
image_id (pk) 
image_name 
image_filesystem_dir 
user_id_fk (this is the user_id from the user table) 
survey_id_fk (this is the survey_id from the survey table) 
... 

// Find all the images for user id 123 and survey 456 
SELECT * FROM image_table AS it, user_table AS ut, survey_table AS st 
JOIN (it.user_id_fk = ut.user_id AND it.survey_id_fk = st.survey_id) 
WHERE user_id_fk = 123 
AND survey_id_fk = 456 

// Pseudo Code 
$user_id  = 123; 
$survey_id = 456; 
$survey_name = 'Stack Survey #1'; 

$image_array = array(
    'image1.png', 
    'image2.png', 
    'image3.png', 
    'image4.png', 
    'image5.png', 
    'image6.png' 
); 

// Folder Structure where User is most searched 
+-+123 <user_id> 
    | 
    +-467 <survey_id> 
    | | 
    | +-images 
    | | 
    | +-image1.jpg 
    | +-image2.jpg 
    | +-image3.jpg 
    +-456 <survey_id> 
    | 
    +-images 
     | 
     +-image1.png 
     +-image2.png 
     +-image3.png 
     +-image4.png 
     +-image5.png 
     +-image6.png 

// Folder Structure where Survey is most searched 
+-+467 <survey_id> 
| | 
| +-123 <user_id> 
| | 
| +-images 
|  | 
|  +-image1.jpg 
|  +-image2.jpg 
|  +-image3.jpg 
+-456 <survey_id> 
    | 
    +-123 <user_id> 
     | 
     +-images 
     | 
     +-image1.png 
     +-image2.png 
     +-image3.png 
     +-image4.png 
     +-image5.png 
     +-image6.png 
0

如果你是用「這樣跟蹤他們的隨着時間的推移改善」建議跟蹤個別的用戶我想有了這樣的頂級目錄開始。但是,即便如此,隨着用戶數量的增加,導致目錄膨脹。

在一個應用程序,我寫,我必須創建系統中的每個用戶的文件時,我寫了一個程序(實際上是數據庫方面),打破了他們唯一的ID成結構化的文件夾。

例如:用戶如果你想要的東西少一點 「猜測的」 12345#上傳5可以存儲在UPLOAD_DIR/1/2/3/4/5/upload_5.png

你仍然可以使用的東西像上面這樣,但不是僅僅使用他們的ID,你可以散列它,但遵循類似的模式。

0

一種替代方法是使用uniqid()隨機地命名文件。您必須檢查以確保uniqid當然不存在。像這樣的東西。

do { 
    $filename = uniqid().'.jpg'; 
} while (!file_exists('images/'.$filename));