如何在 Symfony 1.2 中上传文件而不使用特定表单

发布于 2024-08-15 15:06:53 字数 295 浏览 2 评论 0原文

到目前为止,我还没有在 Symfony 中完成太多文件上传,我正在尝试弄清楚如何在 1.2+ 中执行此操作,

我可以将文件提交给操作并通过以下方式检索它们:

$files = $request->getFiles();

我如何获取然后保存文件到文件系统?除了旧的 1.0 折旧代码之外,我没有看到任何文档。

看起来这已经以某种方式转移到表单系统中,但我没有专门针对这个一次性页面,该页面除了上传一些文件之外什么也不做。

我怎样才能将这些文件保存到光盘上。

I haven't done much file uploading in Symfony to this point and I'm trying to figure out how to do it in 1.2+

I can get the files submitted to the action and retrieve them via:

$files = $request->getFiles();

How can I get then save the file to the file system? I don't see any documentation besides the old 1.0 depreciated code.

It looks like this has been moved somehow into the form system but I don't have a specific for for this one-off page that I have that does nothing but upload a few files.

How can I save these files to disc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瑾兮 2024-08-22 15:06:53

如果您不希望使用 symfony 表单上传文件,那么以下编码也可以使用:

foreach($request->getFiles() as $file)
{
    move_uploaded_file($file["tmp_name"], $directory . "/" . $file['name']);
}

If you are not looking to upload the files using the symfony forms then the following coded will also work:

foreach($request->getFiles() as $file)
{
    move_uploaded_file($file["tmp_name"], $directory . "/" . $file['name']);
}
舟遥客 2024-08-22 15:06:53

在操作内部,将参数绑定到表单后:

  $file = $your_form->getValue('your_file_field');

  $filename  = $file->getOriginalName(); //or whatever name
  $extension = $file->getExtension($file->getOriginalExtension());
  $file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);

顺便说一句,您可以在相应的 Symfony Forms in Action 书中找到 Symfony 1.2 的文档: 第02章文件上传

ps:不要忘记手动添加 enctype="multipart/form-data" 到你的 html 表单!

Inside the action, after you bind the params to the form:

  $file = $your_form->getValue('your_file_field');

  $filename  = $file->getOriginalName(); //or whatever name
  $extension = $file->getExtension($file->getOriginalExtension());
  $file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);

btw, you can find the documentation for Symfony 1.2 in the corresponding Symfony Forms in Action book: Chapter 02 file upload

ps : Don't forget to manually add the enctype="multipart/form-data" to your html form!

谁对谁错谁最难过 2024-08-22 15:06:53

谢谢你的朋友。
我找这个代码已经两天了。即使我正在尝试 symfony,我还是 php 新手。
我的 symfony 代码。

$this->filetype = $_FILES['file']['type'];
foreach ( $request -> getFiles () as  $file )
{ 
    if(move_uploaded_file ( $file [ "tmp_name" ],  sfConfig::get('sf_upload_dir').'/myfile/'.  $file [ 'name' ]))
    { 
        $this-setTemplate('success');
    } 
    else
    {
        $this-setTemplate('failure')
    }
} 

同样的方式使用$filetype来检查上传的文件类型。检查 php 文件参考。对于文件类型。享受..

Thank you friend.
I'm looking for this code for 2 days. I'm new to php even i'm trying symfony.
My code for symfony.

$this->filetype = $_FILES['file']['type'];
foreach ( $request -> getFiles () as  $file )
{ 
    if(move_uploaded_file ( $file [ "tmp_name" ],  sfConfig::get('sf_upload_dir').'/myfile/'.  $file [ 'name' ]))
    { 
        $this-setTemplate('success');
    } 
    else
    {
        $this-setTemplate('failure')
    }
} 

same way use the $filetype to check the uploaded file type. Check php file reference. For file types. Enjoy..

不忘初心 2024-08-22 15:06:53

如果您仍然想知道为什么无法使用 $request->getFiles() 访问上传的文件,可能是因为您忘记添加 enctype="multipart/form-data"

实际上,您应该有一个这样的表单:

<form enctype="multipart/form-data" action="/yourdestination" method="post">
   <input type="file" name="somedummyfile"/>
   <input type="submit" value="Send"/>
</form>

并且您的操作应该执行这样的操作:

$dummyfile = $request->getFiles('somedummyfile');
echo $dummyfile['tmp_name']; // Current filepath
echo $dummyfile['name']; // filename

希望它有帮助!

If you're still looking why you can't access to the uploaded file using $request->getFiles(), it might be because you forgot to add enctype="multipart/form-data".

Actually, you should have a form like this:

<form enctype="multipart/form-data" action="/yourdestination" method="post">
   <input type="file" name="somedummyfile"/>
   <input type="submit" value="Send"/>
</form>

And your action should do something like this:

$dummyfile = $request->getFiles('somedummyfile');
echo $dummyfile['tmp_name']; // Current filepath
echo $dummyfile['name']; // filename

Hope it helps !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文