【问题标题】:Simple File Upload for FuelPHPFuelPHP 的简单文件上传
【发布时间】:2012-11-10 14:09:00
【问题描述】:

有人有使用 FuelPHP 将一系列文件上传到网络服务器的经验吗?

我当前的设置将内容从表单添加到数据库,但我现在也想处理图像 - 所以基本上在提交表单时将它们移动到我的网络服务器。

这很简单吗?

我的控制器中有我的“action_add()”方法,但不确定如何更新它以循环遍历我的所有文件字段并移动文件。

public function action_add()
    {
        $val = Model_Article::validate('add_article');

        if ($val->run())
        {
            $status = (Input::post('save_draft') ? 0 : 1);

            if ( ! $val->input('category_id'))
            {
                $category_id = null;
            }
            else
            {
                $category_id = $val->validated('category_id');
            }

            $article = new Model_Article(array(
                'user_id' => $this->user_id,
                'category_id' => $category_id,
                'title' => $val->validated('title'),
                'body' => $val->validated('body'),
                'published' => $status,
            ));

            if ($article->save())
            {
                Session::set_flash('success', 'Article successfully added.');
            }
            else
            {
                Session::set_flash('error', 'Something went wrong, '.
                    'please try again!');
            }

            Response::redirect('articles/add');
        }

        $this->template->title = 'Add Article';
        $this->template->content = View::forge('articles/add')
            ->set('categories', Model_Category::find('all'), false)
            ->set('val', Validation::instance('add_article'), false);
    }

我的表格:

<h2>Add an Article</h2>
<p>Publish a new article by filling the form below.</p>

<div class="options">
    <div class="option">
            <?php echo Html::anchor('articles', 'View Articles'); ?>
    </div>
    <div class="option">
        <?php echo Html::anchor('categories/add', 'Add a Category'); ?>
    </div>
</div>

<?php echo $val->show_errors(); ?>
<?php echo Form::open(array('enctype' => 'multipart/form-data')); ?>

<?php $select_categories = array(null => 'Uncategorized'); ?>
<?php foreach ($categories as $category): ?>
<?php $select_categories[$category->id] = $category->name; ?>
<?php endforeach; ?>

<div class="input select">
    <?php echo Form::label('Category', 'category_id'); ?>
    <?php echo Form::select('category_id', e($val->input('category_id')), 
        $select_categories); ?>
</div>

<div class="input text required">
    <?php echo Form::label('Title', 'title'); ?>
    <?php echo Form::input('title', e($val->input('title')), 
        array('size' => '30')); ?>
</div>

<div class="input textarea required">
    <?php echo Form::label('Body', 'body'); ?>
    <?php echo Form::textarea('body', e($val->input('body')), 
        array('rows' => 4, 'cols' => 40)); ?>
</div>

<div class="input textarea required">
    <?php echo FORM::file('filename'); ?> 
</div>

<div class="input submit">
    <?php echo Form::submit('add_article', 'Publish'); ?>
    <?php echo Form::submit('save_draft', 'Save Draft'); ?>
</div>

<?php echo Form::close(); ?>

非常感谢您的任何指点。

【问题讨论】:

  • 您只想将图像移动到文件夹或将其保存在数据库中吗?对不起,如果我有点慢,请您也发布您的表格
  • 呵呵,没关系。可能是我的描述 :) 我想将图像移动到我的网络服务器上的文件夹中。然后,我将尝试将文件名移动到我的数据库中的表中。感谢您提供的任何帮助:-D

标签: php file-upload upload fuelphp


【解决方案1】:

好的,我可以给你一些指导。

第一个fuelphp upload documentation

如果有错别字希望对你有所帮助

public function action_add()
{
$val = Model_Article::validate('add_article'); //<-- maybe its just me but I never saw any similar to this in fuelphp sorry about this if I'm wrong

// if your form validation is okay than continue with everyhing else
if ($val->run())
{
    $article = Model_Article::forge();
    // Custom configuration for this upload
    $config = array(
        'path' => DOCROOT.DS.'foldername/tomove/your/images',
        'randomize' => true,
        'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
    );

    Upload::process($config);

    // if a valid file is passed than the function will save, or if its not empty
    if (Upload::is_valid())
    {
        // save them according to the config
        Upload::save();

       //if you want to save to tha database lets grab the file name
        $value = Upload::get_files();  
        $article->your_file_input_name = $value[0]['saved_as'];
     } 

    $status = (Input::post('save_draft') ? 0 : 1);

    if ( ! $val->input('category_id'))
    {
        $category_id = null;
    }
    else
    {
        $category_id = $val->validated('category_id');
    }

         $article->user_id = $this->user_id;
         $article->category_i = $category_id;
         $article->title = $val->validated('title');
         $article->body = $val->validated('body');
         $article->published = $status;


    if ($article->save())
    {
        Session::set_flash('success', 'Article successfully added.');
    }
    else
    {
        Session::set_flash('error', 'Something went wrong, '.
            'please try again!');
    }

    Response::redirect('articles/add');
}

$this->template->title = 'Add Article';
$this->template->content = View::forge('articles/add')
    ->set('categories', Model_Category::find('all'), false)
    ->set('val', Validation::instance('add_article'), false);
}

【讨论】:

  • 谢谢您-我现在就试试,然后向您报告:-)
  • ErrorException [ Parsing Error ]: syntax error, unexpected ')' 在第 87 行:$article-&gt;published = $status; ));
  • 您好,Levente,对于延迟回复,我很抱歉。我回复你后不久,我的网就死了。我现在在办公室,今天午餐时试一试。再次感谢 - 很抱歉耽搁了。
  • 嗨 Levente - 我回来了,测试后 :-) 这是我的错误消息:OutOfBoundsException [ Error ]: Property "your_file_input_name" not found for Model_Article. PKGPATH/orm/classes/model.php @ line 897 有什么想法吗?
  • 不,如果您的输入名称是“文件名”,则将此“您的文件输入名称”重命名为文件名,并在您的文章表所在的数据库中,创建一个名为“文件名”的行,您可以使用数据库也保存
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2014-12-24
  • 2021-11-11
  • 1970-01-01
  • 2019-11-30
相关资源
最近更新 更多