此函数通常在循环内调用,但显然即使不实际使用该函数,仍然存在继续问题。它适用于我更宽容的直播服务器,但我的本地开发系统抗议。如何在不使用continue的情况下告诉它继续下一行?
function deleteImage($ImageName, $UpdateID) {
$array = explode('.', $ImageName);
$ext = end($array);
if ($ext == "pdf") :
$fileTemplate = $_SERVER['DOCUMENT_ROOT'].
DIRECTORY_SEPARATOR . "folder1" .
DIRECTORY_SEPARATOR . "folder2" .
DIRECTORY_SEPARATOR . "%s";
else :
$fileTemplate = $_SERVER['DOCUMENT_ROOT'].
DIRECTORY_SEPARATOR . "folder1" .
DIRECTORY_SEPARATOR . "folder3" .
DIRECTORY_SEPARATOR . "%s";
endif;
if (isset($_POST['update'])):
if (!empty($_POST['CheckBox_Delete'][$UpdateID])) :
$sqlDelete = "DELETE FROM images WHERE ID=$UpdateID";
$sqlFolder = "SELECT EntryID FROM images WHERE ID=$UpdateID";
if (is_null($ImageName) || is_null($UpdateID)) continue;
if (empty($ImageName) || empty($UpdateID)) continue;
$fileName = sprintf($fileTemplate, $ImageName);
DBConnect($sqlDelete, "Delete", "db_name");
// Make sure the file is not in use elsewhere before deleting it from the server
$sqlOtherUse = "SELECT COUNT(ID) FROM images WHERE ImageName='$ImageName' AND ID <> $UpdateID";
$OtherUse = DBLookup($sqlOtherUse, "db_name");
if ($OtherUse < 1) :
@unlink($fileName);
AlertMessage("The file has been deleted!");
else:
AlertMessage("Removed from this entry but the file is in use elsewhere so cannot be deleted.");
endif;
endif;
endif;
}
答案 0 :(得分:2)
您不能在从循环调用的函数中使用continue
,它必须在实际的循环代码中。您可以做的是从函数返回一个值,调用者可以检查它然后继续。
因此,当调用者成为循环的一部分时,请更改调用者:
if (!deleteImage($imagename, $updateid) {
continue;
}
将函数中的所有continue;
语句更改为return false;
,并将return true;
放在函数末尾。
function deleteImage($ImageName, $UpdateID) {
$array = explode('.', $ImageName);
$ext = end($array);
if ($ext == "pdf") :
$fileTemplate = $_SERVER['DOCUMENT_ROOT'].
DIRECTORY_SEPARATOR . "folder1" .
DIRECTORY_SEPARATOR . "folder2" .
DIRECTORY_SEPARATOR . "%s";
else :
$fileTemplate = $_SERVER['DOCUMENT_ROOT'].
DIRECTORY_SEPARATOR . "folder1" .
DIRECTORY_SEPARATOR . "folder3" .
DIRECTORY_SEPARATOR . "%s";
endif;
if (isset($_POST['update'])):
if (!empty($_POST['CheckBox_Delete'][$UpdateID])) :
$sqlDelete = "DELETE FROM images WHERE ID=$UpdateID";
$sqlFolder = "SELECT EntryID FROM images WHERE ID=$UpdateID";
if (is_null($ImageName) || is_null($UpdateID)) return false;
if (empty($ImageName) || empty($UpdateID)) return false;
$fileName = sprintf($fileTemplate, $ImageName);
DBConnect($sqlDelete, "Delete", "db_name");
// Make sure the file is not in use elsewhere before deleting it from the server
$sqlOtherUse = "SELECT COUNT(ID) FROM images WHERE ImageName='$ImageName' AND ID <> $UpdateID";
$OtherUse = DBLookup($sqlOtherUse, "db_name");
if ($OtherUse < 1) :
@unlink($fileName);
AlertMessage("The file has been deleted!");
else:
AlertMessage("Removed from this entry but the file is in use elsewhere so cannot be deleted.");
endif;
endif;
endif;
return true;
}
顺便说一句,没有必要同时测试is_null()
和empty()
,因为空值为空。
答案 1 :(得分:1)
您是否可以撤消对两条continue
行的部分测试并将其转换为代码块?
if (!is_null($ImageName) &&
!is_null($UpdateID) &&
!empty($ImageName) &&
!empty($UpdateID)) {
// code continues here.
}