2017-02-14 40 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); 
相關問題