如何在request->file
中自定义文件上传期间使用$request = Request::createFromGlobals();
从Symfony 2.8
检索pathName到控制器操作,以便从文件中读取数据(我不需要将文件保存到数据库)。如何参考"pathName"
?我无法从上传的文件中检索信息,即我无法获取"pathName"
。
另外,如何与文件一起提交自定义文件名。这可能吗?
//Controller
<?php
namespace CrmBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* @Route("/mip")
*/
class MoInPlController extends Controller {
/**
* Plots profits, etc..
*
* @Route("/m1i1p1d", name="m1i1p1d")
* @Method({"GET","POST"})
*/
public function m1i1p1dAction()
{
$msg[]="";
$request = Request::createFromGlobals();
if ($request->files->get('fileToUpload')) {
if($request->request->get('nameF') ) { $nameF = $request->get('nameF'); }
print_r('<br> 1. request->files->get(fileToUpload) = <pre>'); var_dump($request->files->get('fileToUpload') ); print_r('</pre>');
$ftoup=$request->files->get('fileToUpload');
print_r('<br> 2, ftoup = <pre>'); var_dump( $ftoup ); print_r('</pre>');
print_r('<br> 3, get_object_vars (ftoup) = <pre>'); var_dump( get_object_vars($ftoup) ); print_r('</pre>'); //empty array
print_r('<br> 4, ftoup[pathName] = <pre>'); var_dump( $ftoup["pathName"] ); print_r('</pre>'); //empty string
$msg[]='<br> The file was submitted.';
$strData = file_get_contents($ftoup["pathName"]); //does not work here
$jsonData = json_decode($strData, true);
} else {
$msg[]='<br> Not upload yet.';
}
return $this->render( 'CrmBundle:moinpl:m1i1p1.html.twig',
array( "mssgs"=>$msg ) );
}
...}
Twig模板:
{% block body %}
<form name="crmbundle_moinpl_m1i1p1" method="post" action="/mip/m1i1p1d" enctype="multipart/form-data">
<br> <input type="file" name="fileToUpload" id="fileToUpload">
<br> Name of file for future refering: <input type="text" name="nameF">.
<br> <input type="submit" name="loadF" value="Add File" />
</form>
{%endblock%}
结果如下:
request->files->get(fileToUpload) =
object(Symfony\Component\HttpFoundation\File\UploadedFile)#1066 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(12) "CompSit.json"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(24) "application/octet-stream"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(3259)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(49) "C:\Bitnami\wampstack-5.6.20-0\php\tmp\php3FCA.tmp"
["fileName":"SplFileInfo":private]=>
string(11) "php3FCA.tmp"
}
ftoup =
object(Symfony\Component\HttpFoundation\File\UploadedFile)#1066 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(12) "CompSit.json"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(24) "application/octet-stream"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(3259)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(49) "C:\Bitnami\wampstack-5.6.20-0\php\tmp\php3FCA.tmp"
["fileName":"SplFileInfo":private]=>
string(11) "php3FCA.tmp"
}
get_object_vars (ftoup) =
array(0) {
}
ftoup[pathName] =
答案 0 :(得分:2)
突然间,我放心,我将使用:
$ ftoup = $请求 - &GT;文件 - &GT;获得( 'fileToUpload') - &GT; getPathName();
类"Symfony\Component\HttpFoundation\File\UploadedFile"
的其他有用功能:“getATime”,“getBasename”,“getCTime”,“getClientMimeType”,“getClientOriginalExtension”,“getClientOriginalName”,“getClientSize”,“getError”,“getErrorMessage”, “getExtension”,“getFileInfo”,“getFilename”,“getGroup”,“getInode”,“getLinkTarget”,“getMTime”,“getMaxFilesize”,“getMimeType”,“getOwner”,“getPath”,“getPathInfo”,“getPathname “,”getPerms“,”getRealPath“,”getSize“或”getType“?
symfony官方文档旁边的有用链接:
[https://symfony2-document.readthedocs.io/en/stable[http_fundamentals.html?highlight=request]
[http://www.gdomc.com/0502/how-to-get-the-request-parameters-in-symfony2/]
[http://ebanshi.cc/questions/4002120/publish-a-symfony2-website-webapp]
[http://symfony2-document.readthedocs.io/en/latest/components/http_foundation.html]