2017-02-14 7 views
1

Die Funktion saveImagePath funktioniert nicht, wenn sie sich in der Hauptfunktion befindet uploadFile. Die Datei wird jedoch erfolgreich hochgeladen. Es gibt kein Problem beim Hochladen. Es geht um diese verschachtelte Funktion. Wenn ich den folgenden Code ausführe, wird alles außer dieser `saveImagePath'-Funktion ausgeführt.Warum wird diese Funktion nicht ausgeführt, wenn sie in die Elternfunktion geschrieben wird?

Um dies zu überprüfen, schrieb ich das Problem verursacht verschachtelte Funktion außerhalb der Hauptfunktion und rief es mit $this->saveImagePath und Lo! Es funktionierte. Was kann das Problem sein?

Hat es etwas mit Return-Anweisung zu tun?

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

'Funktion' in' Funktion' macht keinen Sinn. – Mairaj

Antwort

0

Funktion in einer Funktion sieht aus wie eine Klasse für mich, so dass ich den Code als eine Klasse geändert:

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

und jetzt können Sie diese wie verwenden:

(new FileUploader())->uploadFile($fields); 
Verwandte Themen