【问题标题】:Uploading a file with custom generated symfony 1.4 forms使用自定义生成的 symfony 1.4 表单上传文件
【发布时间】:2013-08-20 01:09:18
【问题描述】:

我使用了一个通过模板部分创建自定义 html 表单的模块

我需要管理一个表单的 4 个文件上传。

在操作中,我会像这样提取所有字段数据:

$allData = $request->getParameterHolder()->getAll();

这会给我上传的文件名,但我还需要将该文件保存到上传目录。

我正在尝试像这样绑定表单:

if( isset($customData['getAttachments']) && count($allData)){
        $attachments = $formInput->bind($request->getParameter('forms_input'), $request->getFiles('forms_input'));
}

成功生成$attachments 对象。对此的健全性检查是 var_dump($attachments);die; 转储:

object(FormsInput)#225 (18) { ["_node":protected]=> NULL ["_id":protected]=> array(1) { ["id"]=> string(5) "32171" } ["_data":protected]=> array(6) { ["id"]=> string(5) "32171" ["form_uid"]=> string(32) "aef79b2d47722ca154788cc9c8f8de2b" ["data_array"]=> string(770) "a:20:{s:9:...

如何提取文件并推送到目录。我试过这个:

$file = $attachments->getValue('file');
  if($file) {
  $filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
  $file->save(sfConfig::get('sf_upload_dir').'/'.$formTitle.'/'.$usrDir.''.$filename);
}

但它会抛出此错误:

Unknown record property / related component "value" on "FormsInput"

我怎样才能将上传到 sf 目录?

更新

尝试使用 sfValidatorFile 类,但我不知道如何将 $attachment 对象中的参数传递给它:

$file = new sfValidatorFile();
if($file) {
$filename = sha1($file->getOriginalName()).$file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/uploadedDir');
}

抛出这个:

Fatal error: Call to undefined method sfValidatorFile::save() 

【问题讨论】:

    标签: file-upload symfony-1.4 symfony-forms


    【解决方案1】:

    您打算对 $file->save 执行的操作需要一个 sfValidatedFile 对象。 如果 FormsInput 是 sfFormObject 的一个实例,您可以使用 4 个 sfValidatorFile 配置您的表单,并选择您想要的路径。它会完成您需要的工作。

    【讨论】:

    • 我没有使用传统意义上的 sf 形式。在 FormsInputForms 类中,我没有定义小部件。我对 html 使用部分,然后序列化该表单数据。 $attachments 有文件信息,但我仍然不知道如何从 $attachment 对象中提取它并发送到目录。请参阅上面的更新代码
    • 如果您不使用表单功能,请以“传统的 PHP 方式”(使用 _FILESmove_uploaded_file())保存文件。事实上——如果你不使用小部件和验证,为什么还要使用 Symfony 表单。这只是没有意义。
    • 在许多应用程序中,我们使用 Symfony 表单功能。这恰好是我们需要使用不同方法的特定实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多