我正在尝试编写一个函数来计算图像,同时在一个案例中循环遍历文件(在许多情况下),我$imageCount
在函数中声明了全局变量,copyInsertImage
以便在图像成功插入数据库后,我执行$imageCount++
.
处理完案例的所有图像后,代码退出循环,processImages
再次调用该函数。但是,var_dump($imageCount)
每次图像计数增加一时,我都会打印出图像计数,并发现在$imageCount = 0
为新案例运行循环时,它从未重置回 0。
我想知道声明global $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";
}
}