2012-02-01 208 views
0

作为一个noob ...我不能完全弄清楚什么是不在这里工作.... 如果文件是空的,$this->result包含正确的错误消息。 但是,如果我有一个文件名,结果数组是空的,我没有收到上传php类不执行其他语句

class upload_f 
{ 
    public $path;    // path to upload from root ie files/images/ 
    public $fileName;   // current file ie $_FILES['uploadedfile']['name']; 
    public $result = array(); // array containing error to be loop outside object 

    public function validateInput()  // verify is minimal data is entered. 
    { 

     if (empty($this->fileName)) 
     { 
      $this->result[] = "ERROR: File name is empty."; 
      return false; 
     } 
    } // end of validate 


    public function upload() 
    { 
     // run validation 
     if (!$this->validateInput()) 
     { 
      return $this->result; 
     } 
     else 
     { 
      $f_name = $this->fileName; 
      $path_fileName = $this->path.$this->fileName; 

      if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path_fileName)) 
      { 
       $this->result[] = 
        "File ". basename($_FILES['uploadedfile']['name']). " was uploaded"; 
      } 
      else 
      { 
       $this->result[] = "There was an error uploading the file, please try again!"; 
      } 

      $this->result[] = $path_fileName; 
      return $this->result; 

     } // end of else : upload execution if no errors 
    } 

} // end of upload class 

[...] 

//****************************************/ 

// call the object 
// form here with a if - post ... 
$the_array = $test->upload(); 
$test = new upload_f(); 
// assgin values 
$test->path = "docs/"; 
$test->fileName = $_FILES['uploadedfile']['name']; 
$the_array = $test->upload(); 
echo "<pre>"; 
print_r ($the_array); 
echo "</pre>"; 
+0

为真时,你可能会返回null从validateInput -nothing-代替。 – Alfabravo 2012-02-01 18:24:03

+1

令人惊讶的是,在调试过程中,代码中多少一致的缩进会带给您惊人的效果。当然,**的反**也适用。 – rdlowrey 2012-02-01 18:27:47

+0

您在这里发布的内容中缺少一个括号'}'。 'end of else'和'end class of class'之间应该有一个函数的结尾。我猜这只是一个发布错误。使用更经典的缩进方法可以真正帮助您捕获这些错误。这不仅仅是为了表演。 – 2012-02-01 18:28:41

回答

2

你应该改变validateInput()到:

public function validateInput() {  
    if (empty($this->fileName)) { 
     $this->result[] = "ERROR: File name is empty."; 
     return false; 
    } 
    return true; // <-- return true if input is valid 
} 

当你拥有了它,该方法返回的东西falsy的所有情况,从而导致!$this->validateInput()总是以评估true

参考

+0

嗯...似乎工作感谢...但为什么?如果IF语句有效,它只会变成错误吗? – zefrank 2012-02-01 20:13:56

+0

@zefrank查看该链接。 NULL == false,并且您的方法返回false或NULL。 – paislee 2012-02-01 20:18:01

+0

啊!很棒...每天学习一些东西。自学而且从不使用布尔值。 TKX! – zefrank 2012-02-01 20:27:03

0

不知道如果这是你的问题,但你在你上传结束缺少一个右括号类。

我注意到看着你的代码,你有一个奇怪的风格使用大括号。养成选择风格并坚持不懈的习惯会很棒。只要一致,您可以使用任何风格。这里是他们的列表:Indent Styles

+0

嗯.... ABS右...感谢您的链接! – zefrank 2012-02-01 20:14:23