まず設定ファイルをCore/config/upload.phpからApp/configにコピーします。
<?php
return array(
/**
* global configuration
*/
// if true, the $_FILES array will be processed when the class is loaded
'auto_process' => true,
/**
* file validation settings
*/
// maximum size of the uploaded file in bytes. 0 = no maximum
'max_size' => 0,
// list of file extensions that a user is allowed to upload
'ext_whitelist' => array(),
// list of file extensions that a user is NOT allowed to upload
'ext_blacklist' => array(),
// list of file types that a user is allowed to upload
// ( type is the part of the mime-type, before the slash )
'type_whitelist' => array(),
// list of file types that a user is NOT allowed to upload
'type_blacklist' => array(),
// list of file mime-types that a user is allowed to upload
'mime_whitelist' => array(),
// list of file mime-types that a user is NOT allowed to upload
'mime_blacklist' => array(),
/**
* file save settings
*/
// prefix given to every file when saved
'prefix' => '',
// suffix given to every file when saved
'suffix' => '',
// replace the extension of the uploaded file by this extension
'extension' => '',
// default path the uploaded files will be saved to
'path' => DOCROOT.'assets/upload',
// create the path if it doesn't exist
'create_path' => true,
// permissions to be set on the path after creation
'path_chmod' => 0777,
// permissions to be set on the uploaded file after being saved
'file_chmod' => 0666,
// if true, add a number suffix to the file if the file already exists
'auto_rename' => true,
// if true, overwrite the file if it already exists (only if auto_rename = false)
'overwrite' => false,
// if true, generate a random filename for the file being saved
'randomize' => false,
// if true, normalize the filename (convert to ASCII, replace spaces by underscores)
'normalize' => false,
// valid values are 'upper', 'lower', and false. case will be changed after all other transformations
'change_case' => false,
// maximum lengh of the filename, after all name modifications have been made. 0 = no maximum
'max_length' => 0
);
変えたところは path くらいです。
次にコントローラ。
<?php
class Controller_Upload extends Controller_Template
{
public function action_index()
{
$data = array();
$errors = array();
$files = array();
// POSTを確認
if (Input::method() == 'POST')
{
Upload::process();
if (Upload::is_valid())
{
// アップロード
Upload::save();
// エラーメッセージを取得する
foreach (Upload::get_errors() as $file)
{
foreach ($file['errors'] as $error)
{
$errors[] = $error['message'];
}
}
}
}
// config/upload.phpの読み込み
Config::load('upload', true);
$upload_config = Config::get('upload');
// ファイル一覧を取得する
$files = File::read_dir($upload_config['path']);
$data['errors'] = $errors;
$data['files'] = $files;
$this->template->title = 'ファイルアップロード';
$this->template->content = View::forge('upload/form', $data);
}
}
やっていることとしてはファイルのアップロードとアップロードディレクトリからファイル一覧を取得してるくらいです。
といいながらもファイル一覧で結構悩みました。。。
最後にViewです。
<-- errors -->
<?php if (count($errors) > 0) : ?>
<ul>
<?php foreach ($errors as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php echo Form::open(array('class'=>'form-stacked', 'enctype'=>'multipart/form-data')); ?>
<div class="clearfix">
<?php echo Form::label('画像ファイル', 'upload-file'); ?>
<div class="field">
<?php echo Form::file('upload-file'); ?>
</div>
</div>
<div class="actions">
<?php echo Form::submit('submit', 'アップロード', array('class' => 'btn primary')); ?>
</div>
<?php echo Form::close(); ?>
<-- files -->
<ul>
<?php foreach ($files as $file) : ?>
<li><?php echo $file; ?></li>
<?php endforeach; ?>
</ul>
真ん中にフォームが上と下にそれぞれエラーメッセージとファイル一覧を表示するようにしています。
form タグの指定が通常とは違うんで注意するところはそれぐらいかなと思います。

0 件のコメント:
コメントを投稿