2017-02-14 43 views
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'].'"}'; 
     } 
    } 
+2

'function'里面的'function'没有任何意义。 – Mairaj

回答

0

功能的函数看起来像一个类来我,所以我已经改变了你的代码是一个类:

class FileUploader { 
    public function uploadFile($fields){ 
     $files = $_FILES[ 'photo' ]; 
     $count = count($files[ 'name' ]); 
     if ($fields[ 'type' ] == "PROFILEPIC") { 
      $folderName = "userimages/"; 
     } else { 
      $folderName = "personalmarkerimages/"; 
      $transportStopId = $fields[ 'transportStopId' ]; 
     } 

     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) { 
          $this->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'].'"}'; 
     } 
    } 

    protected 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(); 
    } 
} 

,现在你可以使用此类似:

(new FileUploader())->uploadFile($fields); 
相关问题