2015-09-02 50 views
0

目前我的根文件夾中有一個名爲showcase的目錄。上傳文件時,我想檢查目錄是否存在,如果不存在,請根據當前日期創建它,然後將文件移動到該文件夾​​。根據當前日期創建目錄

$dateYear = date('Y'); 
$dateMonth = date('M'); 
$dateDay = date('d'); 

if (!is_dir("/showcase/$dateYear/$dateMonth/$dateDay")) { 
    mkdir("/showcase/$dateYear/$dateMonth/$dateDay");   
} 

if (move_uploaded_file($fileTmpLoc,"/showcase/$dateYear/$dateMonth/$dateDay/$newName")){ 

    // stuff 

} 

$newName是文件名,例如, SajdaT.jpg。這段代碼對我無能爲力。我怎樣才能創造出我想要的東西?

例如

/showcase/2015/09/02創建,如果它不存在,然後將文件移動到它像

/showcase/2015/09/02/SajdaT.jpg

+0

每當有文件,檢查權限工作的第一位。查看錯誤日誌並查看是否存在有關權限的錯誤。幾乎每次有人說「它什麼都不做」時,實際上就是這樣,它會向錯誤日誌中吐出大量錯誤。 – kainaw

+0

'is_dir'和'mkdir'行看起來更像我的URL,你真的有一個名爲'showcase'的文件夾在你的文件系統的根目錄嗎? (即你知道你正在使用絕對路徑,對嗎?)O_o –

+0

'PHP警告:mkdir():沒有這樣的文件或目錄在/ home/*****/public_html/****/upload。 91行上的php – frosty

回答

1
Pass recursive attribute as true with the method. 

<?php 

// Desired folder structure 
$structure = './dir1/dir2/dir3/'; 

// To create the nested structure, the $recursive parameter 
// to mkdir() must be specified. 

if (!mkdir($structure, 0777, true)) { 
    die('Failed to create folders...'); 
} 

// ... 
?> 
+0

我得到'mkdir():Permission denied' - 這是在一個共享主機 – frosty

+0

耶可能是。只需檢查您作爲showcase目錄中用戶的權限。您需要擁有寫入權限才能創建一個。 –

+0

沒關係我得到它的工作! :) – frosty