LoginSignup
5
3

More than 5 years have passed since last update.

Laravel4でのファイル入力から移動まで

Last updated at Posted at 2013-09-12

軽く覚書的なもの。

Larave4を使って少しやった。

views/test.php
<!doctype html>
<html lang="ja-JP">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?= Form::open(array('url' => '' ,'files' => true)) ?>
    <input name="image" type="file" />
    <input type="submit">
    <?= Form::close() ?>
</body>
</html>
controller/HomeController.php
<?php
public function showTest()
{
    return View::make('test');
}

public function execTest()
{
    $set_path = public_path('images/');

    // ディレクトリは存在するか確認
    if(!File::exists($set_path))
    {
        File::makeDirectory($set_path);
    }

    // ファイルは入力されたか
    if(!Input::hasFile('image'))
    {
        return;
    }
    $file = Input::file("image");

    // 情報を取得
    $file_path = $file->getRealPath();
    $name = $file->getClientOriginalName();

    // ファイルをtmpから移動
    File::move($file_path, $set_path . $name);

    // 以降は。。。。
    exit();
}
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3