数组到字符串转换的麻烦

时间:2014-06-25 18:27:37

标签: php arrays oop foreach upload

所以我正在努力处理我正在开发的项目的代码。我不明白为什么我在下面使用Move_uploaded_file函数时得到一个数组到字符串转换错误,因为我实现了一个foreach循环只能处理数组中的每个单独元素。还要注意问题 - 根据php的错误处理 - 特别是使用move_uploaded_file函数,而不是调用其他方法。

这是相关代码。 (谢谢大家的帮助)。

public function relocate () {

    foreach ($this->getFilename() as $name) {
        $validate = $this->validatePhoto($name); 
        $size = $this->getSize($name); 

        if ($validate && $size) {
            if (move_uploaded_file($name, $this->filepath . $this->getFilename())) {
                echo "<p> upload complete </p>"; 
                //rename file, redirect header, etc. 
            } //end if move_uploaded_file
            else {
                echo "<p> something's up. </p>"; 
            }//end else 
        }//end if validate && size
    }//end foreach
}//end relocate method

2 个答案:

答案 0 :(得分:0)

应该是

public function relocate () {

    foreach ($this->getFilename() as $name) {
        $validate = $this->validatePhoto($name); 
        $size = $this->getSize($name); 

        if ($validate && $size) {
            if (move_uploaded_file($name, $this->filepath . $name)) {
                echo "<p> upload complete </p>"; 
                //rename file, redirect header, etc. 
            } //end if move_uploaded_file
            else {
                echo "<p> something's up. </p>"; 
            }//end else 
        }//end if validate && size
    }//end foreach
}//end relocate method

当你要传递$this->getFileName()时,你已经move_uploaded_file()传递给$name,因为你已经在$this->getFileName()的数组上进行了迭代正在返回

答案 1 :(得分:0)

你应该用$this->getFilename()替换$name$this->getFilename()如果我做对了就会返回一个数组......

public function relocate () {

    foreach ($this->getFilename() as $name) {
        $validate = $this->validatePhoto($name); 
        $size = $this->getSize($name); 

        if ($validate && $size) {
            if (move_uploaded_file($name, $this->filepath . $name)) {
                echo "<p> upload complete </p>"; 
                //rename file, redirect header, etc. 
            } //end if move_uploaded_file
            else {
                echo "<p> something's up. </p>"; 
            }//end else 
        }//end if validate && size
    }//end foreach
}//end relocate method