作为一个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