開発メモ

開発用のメモです。

Cakephp FileUpload で DBにファイルを保存

<?php
public function edit($id) {
    if ($this -> request -> is("PUT")) {
        foreach (array("image1", "image2") as $key) {
            if (isset($this -> request -> data["Product"][$key])) {
                if ($this -> request -> data["Product"][$key]["error"] != 0) {
                    $flag = true;
                    switch($this -> request -> data["Product"][$key]["error"]) {
                        case UPLOAD_ERR_INI_SIZE :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_INI_SIZE"));
                            break;
                        case UPLOAD_ERR_FORM_SIZE :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_FORM_SIZE"));
                            break;
                        case UPLOAD_ERR_PARTIAL :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_PARTIAL"));
                            break;
                        case UPLOAD_ERR_NO_FILE :
                            unset($this -> request -> data["Product"][$key]);
                            $flag = false;
                            break;
                        case UPLOAD_ERR_NO_TMP_DIR :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_NO_TMP_DIR"));
                            break;
                        case UPLOAD_ERR_CANT_WRITE :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_CANT_WRITE"));
                            break;
                        case UPLOAD_ERR_EXTENSION :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_EXTENSION"));
                            break;
                        default :
                            $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_OTHER"));
                            break;
                    }
                    if ($flag) {
                        $this -> render("form");
                        return;
                    }
                } else if ($this -> request -> data["Product"][$key]["size"] == 0) {
                    $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_NO_FILE"));
                    $this -> render("form");
                    return;
                } else if ($this -> request -> data["Product"][$key]["size"] > (3 * 1024 * 1024)) {
                    $this -> Session -> setFlash(__d("products", "UPLOAD_ERR_FORM_SIZE"));
                    $this -> render("form");
                    return;
                } else if (strcmp($this -> request -> data["Product"][$key]["type"], "image/jpeg") != 0) {
                    echo $this -> request -> data["Product"][$key]["type"];
                } else {
                    $this -> request -> data["Product"][$key] = file_get_contents($this -> request -> data["Product"][$key]["tmp_name"]);
                }
            }
        }
        if ($this -> Product -> save($this -> request -> data)) {
            $this -> Session -> setFlash(__d("products", "Update Success"));
        } else {
            $this -> Session -> setFlash(__d("products", "Update Error"));
        }
    } else {
        $this -> request -> data = $this -> Product -> findById($id);
    }
    $this -> render("form");
}
Twitter: @asahina_alice