作爲一個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>";
爲真時,你可能會返回null從validateInput -nothing-代替。 – Alfabravo 2012-02-01 18:24:03
令人驚訝的是,在調試過程中,代碼中多少一致的縮進會帶給您驚人的效果。當然,**的反**也適用。 – rdlowrey 2012-02-01 18:27:47
您在這裏發佈的內容中缺少一個括號'}'。 'end of else'和'end class of class'之間應該有一個函數的結尾。我猜這只是一個發佈錯誤。使用更經典的縮進方法可以真正幫助您捕獲這些錯誤。這不僅僅是爲了表演。 – 2012-02-01 18:28:41