1
当功能saveImagePath
位于主功能uploadFile
内时,它不起作用。但是文件上传成功。上传没有问题。这完全是关于这个嵌套函数。当我执行下面的代码时,除了`saveImagePath'函数之外,所有内容都会执行。为什么这个函数在父函数中写入时不执行?
为了验证这一点,我编写了在主函数之外引起嵌套函数的问题,并使用$this->saveImagePath
和Lo!有效。这背后的问题是什么?
它与return语句有关吗?
function uploadFile($fields){
$files = $_FILES['photo'];
$count = count($files['name']);
if($fields['type'] == "PROFILEPIC"){
$folderName="userimages/";
}else{
$folderName="personalmarkerimages/";
$transportStopId=$fields['transportStopId'];
}
function saveImagePath($imagePath, $transportStopId)
{
$db=$this->dbConnect();
$mySqlQuery = "UPDATE mytablename SET imagePath=:imagePath WHERE filedname=:stopId";
$stmt=$db->prepare($mySqlQuery);
$stmt->bindParam("imagePath", $imagePath);
$stmt->bindParam("stopId", $transportStopId);
$stmt->execute();
}
if(gettype($files['name'])=='array'){
$num=0;
for($i = 0 ; $i < $count ; $i++) {
if ($files['error'][$i] === 0) {
$target_file='/myaddress/' . $folderName . $files['name'][$i];
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo '{"status": "error", "message": "Not a valid format"}';
}else{
if (file_exists($target_file)) {
echo '{"status": "error", "message": "File already exist"}';
break;
}else if(move_uploaded_file($files['tmp_name'][$i], $target_file) === true) {
saveImagePath($target_file, $transportStopId);
$num=$i+1;
}
else{
}
}
}
}
}else{
echo '{"status": "error", "message": "Couldn\'t upload"}';
}
if($num==$count){
echo '{"status": "success", "values": "'.$fields['description'].'"}';
}
}
'function'里面的'function'没有任何意义。 – Mairaj