2011-09-29 120 views
2

我想讓我的代碼工作。我基本上檢查,如果一個文件夾存在,並且該目錄中存在任何子文件夾稱爲3或更多(3,4,5),然後做一個循環,否則沒有。PHP如果文件夾存在爲每個子文件夾做一個循環

// the folder path 
$folderPath="".cdnurl."assets/".pid.""; 
// looks like site.com/assets/342/ 

// is there a sub folder called 3 or more, 3,4,5 and loop from 3 
// site.com/assets/342/3 
// site.com/assets/342/4 
// etc, we only loop if 3 exists other wise nothing 

$folderSubNumber =>3; 

    while(file_exists($folderPath.$folderSubNumber)) { 

     echo '<li><img src="assets/pid/'.folderSubNumber.''; 

    } else { 

    // nothing 
     echo "";  
    } 
+0

而你的問題是什麼? – genesis

+2

while(){} else {}''''''''''''''''''''''''''''''''''''''echo'''''''''''''''$ folderSubNumber => 3;'$ folderSubNumber = 3; '('=>'僅適用於數組鍵/值)。 –

+0

PHP沒有while(){} else {}語句,請嘗試刪除else {}並查看會發生什麼。 – csjohn

回答

0
// Build folder path 
$folder_path = cdnurl . 'assets/' . $pid . '/'; 

// Loop from 3 to 5 
for ($i = 3; i <= 5; $i++) { 

    // Checking for a valid sub-directory 
    // Will check sub-directory e.g.: site.com/assets/342/3/ 
    if (is_dir($folder_path . $i . '/')) { 

    // Sub-directory exists 
    echo $folder_path . $i; 
    } else { 

    // No Sub-directory, break out of for loop 
    // This is will stop PHP for looking for sub-directory 4 or 5 
    break; 
    } 
} 
2

一個簡單的方法:

$subdirs = glob(cdnurl . "assets/" . pid . "/*", GLOB_ONLYDIR); 

那將返回所有子目錄中的指定目錄下。參考文獻:http://php.net/glob

+0

但是,這隻對名爲「3」或更高的子目錄沒有幫助。 – sdleihssirhc

+0

這是一個unix-shell風格的模式匹配。如果你想要以3,4,5,6,7,8,9開頭的文件,那麼對於glob模式執行'yadayada/[3456789] *'。 –

3

它看起來像你應該做的是什麼。只是改變=>一個簡單=,並且不要忘記遞增:

while (file_exists($folderPath.$folderSubNumber)) { 
    echo '...'; 
    $folderSubNumber += 1; 
} 

(此外,else部分是不允許的,但你不要在這裏需要它,所以沒有丟失。)

+0

忘記了@Helgi提到的文件分隔符:應該是'$ folderPath。'/'。$ folderSubNumber' – sdleihssirhc

1

請準備一些建設性的批評。

有很多不對的代碼。首先,我會添加評論來解釋一些錯誤以及一些毫無意義的事情。


$folderPath="".cdnurl."assets/".pid.""; 
// 1. Single-quotes will perform slightly better. 
// 2. There is no need for the first "". or the final ."" - they do nothing. 
// 3. Ideal: $folderPath = cdnurl.'assets/'.pid; 
// 4. This assumes that cdnurl and pid are constants declared with the define() command. If they are not constants, you need dollar-signs, which would make it: 
// $folderPath = $cdnurl.'assets/'.$pid; 

$folderSubNumber => 3; 
// You cannot put a "more than X" or "less than X" in a variable. The => is used in foreach() loops for a completely different purpose, and when declaring values in an array only when the array is originally declared. (In other words; in this case, this does nothing.) 

// Indentation really does matter. This should be indented the same as the code above. 
while(file_exists($folderPath.$folderSubNumber)) { 
    // 1. $folderSubNumber never changes and so this while-loop always asks the exact same question. 
    // 2. You don't have a directory separator "/", so this will append $folderSubNumber straight to pid, above. 

    echo '<li><img src="assets/pid/'.folderSubNumber.''; 
    // 1. folderSubNumber needs a dollar-sign because it's a variable. If it is not defined as a constant, it will simply be the literal string "folderSubNumber". 
    // 2. The appended .'' does nothing and shouldn't be there. 
    // 3. You are neither closing the <img> tag, nor the <li> tag, nor in fact the src-attribute. 
    // 4. Ideal: echo '<li><img src="assets/pid/'.$folderSubNumber.'" /></li>'; 

} else { 
    // 1. There is no "else" in while. 
    // 2. You don't need an "else" if the intention is to do nothing. 

    echo ""; 
    // This is 100% pointless, it does nothing. 
} 

那麼你需要什麼,是遞增$ foldeSubNumber您在while循環試過之後(見「sdleihssirhc」的答案)。但是請注意,您可能需要$ folderPath和$ folderSubNumber之間的目錄分隔符。這將是:$folderPath.'/'.$folderSubNumber

祝你好運!

相關問題