1

ユーザーの写真をアップロードしようとしていますが、次の例ではデータベースに何も保存されず、エラーも発生しません。検証を行う必要があることはわかっています。ファイルを保存すると、検証が行われます。

ビュー ファイルのスニペットは次のとおりです。

<?php 
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->form->input('upload', array('type' => 'file')); 
echo $this->Form->end('Submit');
?>

コントローラー:

public function add() {
    if ($this->request->is('post')) {
        if(!empty($this->data['User']['upload']['name'])){
             $file = $this->data['User']['upload'];
             move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/users/' . $file['name']);
             $this->data['User']['image'] = $file['name'];
        }                   
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash('The employee has been saved');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('The employee could not be saved. Please, try again.');
        }
    }
}
4

2 に答える 2

1

次のリンクを確認してください。

http://www.jamesfairhurst.co.uk/posts/view/uploading_files_and_images_with_cakephp

public function uploadFilesIphone($folder, $formdata, $replace , $itemId = null) {
    // setup dir names absolute and relative

    $folder_url = WWW_ROOT.$folder;
    $rel_url = $folder; //echo

    // create the folder if it does not exist
    if(!is_dir($folder_url)) {
        mkdir($folder_url);
    }

    // if itemId is set create an item folder
    if($itemId) {
        // set new absolute folder
        $folder_url = WWW_ROOT.$folder.'/'.$itemId; 
        // set new relative folder
        $rel_url = $folder.'/'.$itemId;
        // create directory
        if(!is_dir($folder_url)) {
            mkdir($folder_url);
        }
    }

    // list of permitted file types, this is only images but documents can be added
    $permitted = array('image/gif','image/jpeg','image/pjpeg','image/png','application/octet-stream');

    // loop through and deal with the files;

    $key = array();
    $value = array();
    foreach($formdata as  $key => $value) 
    {   
        if($key == is_array($value))
        {
            $filename = str_replace(".", $replace , $value['name']);
        }   

        // replace spaces with underscores

        // assume filetype is false
        $typeOK = false;
        // check filetype is ok

        foreach($permitted as $type) 
        {   
            if($key == is_array($value))
            {
                if($type == $value['type']) 
                {
                    $typeOK = true;
                    break;
                }
            }   
        }
        // if file type ok upload the file

        if($typeOK) {
            // switch based on error code
            if($key == is_array($value))
            {
                switch($value['error']) 
                {
                    case 0:
                        // check filename already exists
                        if(!file_exists($folder_url.'/'.$filename)) 
                        {
                            // create full filename
                            $full_url = $folder_url.'/'.$filename;
                            $url = $rel_url.'/'.$filename;

                            // upload the file
                            if($key == is_array($value))
                            {
                                $success = move_uploaded_file($value['tmp_name'], $url);
                            }
                        } 
                        else 
                        {
                            // create unique filename and upload file
                            //  ini_set('date.timezone', 'Europe/London');
                            $now = date('Y-m-d-His');
                            $full_url = $folder_url.'/'.$now.$filename;
                            $url = $rel_url.'/'.$now.$filename;
                            if($key == is_array($value))
                            {   
                                $success = move_uploaded_file($value['tmp_name'], $url);
                            }
                        }
                        // if upload was successful
                        if($success) 
                        {
                            // save the url of the file
                            $result['urls'][] = $url;
                        } 
                        else 
                        {
                            $result['errors'][] = "Error uploaded $filename. Please try again.";
                        }
                        break;
                    case 3:
                        // an error occured
                        $result['errors'][] = "Error uploading $filename. Please try again.";
                        break;
                    default:
                        // an error occured
                        $result['errors'][] = "System error uploading $filename. Contact webmaster.";
                        break;
                }
            }
            elseif($value['error'] == 4) 
            {
                // no file was selected for upload
                $result['nofiles'][] = "No file Selected";
            } 
            else 
            {
                // unacceptable file type
                $result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
            }
        }
    }
    return $result;
}
于 2013-09-10T09:58:11.960 に答える