无法回显数组

时间:2015-08-30 16:19:44

标签: php

我正在尝试制作照片上传页面。上传功能很好;但是当我试图触发一些错误时,例如不选择任何要更新的文件,我看不到错误信息;相反,页面变为空白。

这是我的代码(我省略了一些属性和方法):

Class Photo {
public $errors = array();
public $upload_error = array(

    UPLOAD_ERR_OK           =>"Upload Success.",
    UPLOAD_ERR_INI_SIZE     =>"The upload file exceeds the upload_max_filesize",
    UPLOAD_ERR_FORM_SIZE    =>"The upload file exceeds the max file size",
    UPLOAD_ERR_PARTIAL      =>"The upload was only partially uploaded.",
    UPLOAD_ERR_NO_FILE      =>"No file was uploaded",
    UPLOAD_ERR_NO_TMP_DIR   =>"Missing a temporary folder.",
    UPLOAD_ERR_CANT_WRITE   =>"Failed to write file to disk",
    UPLOAD_ERR_EXTENSION    =>"A PHP extension stopped the file upload."

    );
    public function set_file($file){

    if(empty($file) || !$file || !is_array($file)){
        $this->errors[] = "There was no file uploaded here";
        return false;

    } elseif ($file['error'] !=0){
        $this->errors[] = $this->$upload_error[$file['error']];
        return false;

    } else {

    $this->filename = basename($file['name']);
    $this->tmp_path = $file['tmp_name'];
    $this->photo_type = $file['type'];
    $this->size = $file['size'];

    }

        public function save() {

    if ($this->id) {
        $this->update();

    } else {

        if (!empty($this->errors)) {

            return false;
        }

        if (empty($this->filename) || empty($this->tmp_path)){
            $this->errors[] = "the file was not available!";
        }

        $target_path = SITE_ROOT . DS . 'admin' . DS . $this->upload_directory . DS . $this->filename;


        if (file_exists($target_path)) {
            $this->errors[] = "The file {$this->filename} already existed.";
            return false;
        }

        if (move_uploaded_file($this->tmp_path, $target_path)) {

            if ($this->create()) {
                unset($tmp_path);
                return true;
            } else {
                $this->errors[] = "You cannot access the file diretory.";
                return false;
                foreach ($this->errors as $error) {
                    echo $error."<br/>";
                }
            }

        } // if move upload file

    }

} // End Save method

    }

上传页面为:

    $message = "";

if (isset($_POST['submit'])) {

    $photo = new Photo();
    $photo->title = $_POST['title'];
    $photo->set_file($_FILES['file_upload']);

if ($photo->save())

    $message = "Photo upload successfully";

} else {

    $message = join("<br/>", $photo->errors);
}
  • 我可以看到成功消息“照片上传成功”,但没有错误消息。

有点乱,但是有人可以帮我这个吗?谢谢!

- UPDATE -

在$ this-&gt; $ upload_error [$ file ['error']]中发现拼写错误; (应该是$ this-&gt; upload_error),谢谢你的帮助!但是我仍然看不到错误消息。

1 个答案:

答案 0 :(得分:0)

       else {
            $this->errors[] = "You cannot access the file diretory.";
            return false;
            foreach ($this->errors as $error) {
                echo $error."<br/>";
            }

这不起作用的原因是因为您从方法返回,然后尝试运行foreach循环。从方法/函数return开始,该方法/函数内的执行结束。尝试将foreach 放在 return语句之前。