2014-12-07 2 views
1

나는이 코드 블록을 제대로 실행하지 못한다.왜 처음 if 문이 항상 실행됩니까?

private function verifyImage() { 
    if (!is_null($this->uploads) && array_key_exists('image', $this->uploads)) { 
     $image = $this->uploads['image']; 
     $tmpPath = $image['tmp_name']; 
     if (!empty($tmpPath)) { 
      $newName = $this->userName . "." . pathinfo($image['name'],PATHINFO_EXTENSION); 
      move_uploaded_file($tmpPath, __ROOT__ . '/images/' . $newName); 
      $this->image = __WEBROOT__ . '/images/' . $newName; 
     } 
    } elseif (isset($this->formInput['currentImage'])) { 
     $this->image = trim($this->formInput['currentImage']); 
    } elseif (isset($this->formInput['image']) && !empty($this->formInput['image'])) { 
     $this->image = trim($this->formInput['image']); 
    } else { 
     $this->setError('image',"Error with image field"); 
    } 
} 

$ this-> 업로드 $입니다 HTML 포스트에서 _FILES

$ this-> formInput는 HTML 포스트에서 $ _POST입니다

문제는 불을 지르고 쇼 숨겨진 필드 'currentImage'함께 그것은 분명하게 설정되어있다. 그러나 끝에있는 else 루프가 설정됩니다. 그것은 단지 $ _POST에 대해 true를 반환 [ 'currentImage'] 나는 다음과 같이 코드를 변경하면 설정되는 :

에서 : } elseif (isset($this->formInput['currentImage'])) {

에 : 그래서 } if (isset($this->formInput['currentImage'])) {

ELSEIF 또는 false를 반환 '다른 경우', 하지만 간단한 if가 true를 반환합니까? 당신은 단순히 여기에서 이해할 필요가 무엇

+0

소리가 난다 else 조건을 검사조차하지 않습니다. – dave

+0

첫 번째 if는 true를 반환하므로 elseif는 실행되지 않습니다. – MisterBla

+0

'isset' 연산의 결과를 캐싱하여 코드를보다 쉽게 ​​읽을 수 있도록 코드를 단순화하는 것이 좋습니다. – Dai

답변

1

이전 if 문이 거짓 인 경우 elseif 블록 내부에 아무것도 를 실행한다는 것입니다.

그러나 간단한 if 문으로 변경하면 if 문 내부의 지정된식이 참인지 여부 만 확인합니다.

if (!is_null($this->uploads) && array_key_exists('image', $this->uploads)) {은 PHP에서 양식에 파일 입력이 있거나없는 경우 FILES가 설정되어 있기 때문에 true가됩니다. 그래서 옆에있는 elseif 문에 도달하지 않습니다.

파일이 될 것이다, 또는 업로드되지 않은 경우 확인하는 올바른 방법,

if($this->uploads['image']['error'] != 0) 
{ 
    // If a upload is set, this will be executed 

    $image = $this->uploads['image']; 
    $tmpPath = $image['tmp_name']; 
    if (!empty($tmpPath)) { 
     $newName = $this->userName . "." . pathinfo($image['name'],PATHINFO_EXTENSION); 
     move_uploaded_file($tmpPath, __ROOT__ . '/images/' . $newName); 
     $this->image = __WEBROOT__ . '/images/' . $newName; 
    } 
} 
elseif (isset($this->formInput['currentImage'])) { 
    $this->image = trim($this->formInput['currentImage']); 
} elseif (isset($this->formInput['image']) && !empty($this->formInput['image'])) { 
    $this->image = trim($this->formInput['image']); 
} else { 
    $this->setError('image',"Error with image field"); 
} 

자세한 내용은 방문 첫 번째 조건이 너무 사실처럼 http://php.net/manual/en/reserved.variables.files.php

+0

그래, 나는 모든 사람이 게시 한 주석과 다양한 코드 테스트에서 항상 설정된다는 것을 알았습니다. 업로드 오류를 확인하는 방법을 보여 주셔서 감사합니다. 나는 실제로 첫 번째 if 문에 isset ($ this-> formInput [ 'image'])을 추가하여 작동 시키지만 실제로 테스트하는 좋은 방법은 분명합니다. 나는 그것이 잘 설정되어 있기 때문에 else가 유일한 코드 실행 블록이라고 생각했다. 코드를 두 번 이상 실행해야합니다. –

0
// php constant UPLOAD_ERR_OK,Value: 0; There is no error, the file uploaded with success. 
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { 
// do uploading 
} else { 
// your error msg goes here. 
}