2011-09-29 2 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

'() {} else {}','folderSubNumber'는'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); 

지정된 디렉토리의 모든 서브 디렉토리를 돌아갑니다. Ref : http://php.net/glob

+0

하지만 "3"이상의 이름을 가진 하위 디렉터리만으로는 도움이되지 않습니다. – sdleihssirhc

+0

유닉스 쉘 스타일 패턴 일치입니다. 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를 증가 ('sdleihssirhc'로 답 참조)하는 것입니다. 그러나 $ folderPath와 $ folderSubNumber 사이에 디렉토리 구분자가 필요할 수도 있습니다. 즉 : $folderPath.'/'.$folderSubNumber

행운을 빕니다!

관련 문제