하나의 사례 (많은 경우)에 대한 파일을 반복하면서 이미지를 계산하는 함수를 작성하려고 시도 했으므로 이미지 내에서 copyInsertImage
변수 내에서 변수 $imageCount
을 전역으로 선언했습니다. 데이터베이스에 성공적으로 삽입되면 $imageCount++
을 수행합니다.PHP 전역 카운트가 재설정되지 않습니다.
사례의 모든 이미지를 처리 한 후 코드가 루프를 종료하면 processImages
함수가 다시 호출됩니다. 그러나 이미지 수가 증가 할 때마다 var_dump($imageCount)
을 인쇄하여 새로운 케이스의 루프를 실행할 때 $imageCount = 0
에 의해 다시 0으로 재설정되지 않았 음을 알았습니다.
global $imageCount
이 코드와 아무 관계가 없는지 궁금합니다. 동일한 스크립트를 함수로 그룹화하기 전에 코드가 제대로 작동했기 때문입니다. 그렇다면 솔루션은 무엇입니까?
감사합니다. 내가 제대로 질문을 이해하면 당신은 당신의 "copyInsertImage"기능의 내부에 "글로벌 $의 imageCount"를 재 선언하는 것처럼
function processImages($path,$patientID,$caseID)
{
// $path = rootDirectory/patientID
global $targetDirectory;
$targetPath = $targetDirectory.$patientID;
$caseSummaryFolder = $path."/".$caseID."/Summary";
$srcDirPath=$path."/".$caseID."/Summary/slicesdir"; //AKA src
$dstDirPath = $targetPath."/".$caseID;
//copyCaseImages($caseSummaryFolder,$targetCasePath,$patientID,$caseID);
global $status;
// print("processImages case path:".$casePath."</br>");
$files = glob($srcDirPath."/*.png");
echo "\n------------NEW CASE------------\n"
echo "PATIENT: $patientID \n";
echo "CASE: $caseID \n";
echo "--------------------------------\n"
$imageCount = 0;
for($i = 0; $i < count($files); $i++) {
$file = $files[$i];
$fileName = str_ireplace($srcDirPath."/", "", $file);
// if image name doesn't not contain string 'GROT'
if(strripos($fileName, "grot") === false)
{
if(doesImgExist($fileName)!==NULL) {
if (compareFileMTime($srcDirPath,$fileName,doesImgExist($fileName))) {
echo "There's a newer version of $fileName \n";
copyInsertImage($srcDirPath,$dstDirPath,$fileName,$patientID,$caseID);
}
else {
$imageCount++;
}
}
// copy image to analyzedCp and insert new image into DB
else {
copyInsertImage($srcDirPath,$dstDirPath,$fileName,$patientID,$caseID);
}
}
else {
echo "grot*.png files are not included \n";
}
}
글로벌 변수로 바꾸는 대신 해당 변수를 함수에 전달하지 않는 이유는 무엇입니까? 그런 다음 업데이트 된 $ imageCount를 반환하게하십시오 – JRizz
'$ imageCount'를'processImages()'에서 전역으로 선언 할 필요가 없습니까? 그렇지 않으면, 당신은 단지 지역 변수를 사용하고 있습니다. – andrewsi
그래서 $ imageCount는 전역으로 정의되어 있습니까? –