2017-02-14 3 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); 
関連する問題