我正在尝试从要上传到服务器的FormData对象中获取原始文件名,但是我一生都无法找到如何访问该文件名的方法。
我的js / ajax就像这样:
function AjaxFileUpload(files, progressBar, target, id, imageContainer){
//Creates a formdata object for the upload, appends a CSRF token, the file itself and its respective name
var formData = new FormData;
formData.append('_token', CSRF_TOKEN);
formData.append('target', target);
formData.append('id', id)
for(var i = 0; i < files.length; i++){
formData.append(files[i].name, files[i]);
}
$.ajax({
url: '/upload',
type: 'POST',
data: formData,
cache: false,
contentType: false,
processData: false,
它在服务器端使用我的上载控制器进行处理,如下所示:
public function upload(Request $request){
$arr = [];
foreach($request->all() as $file){
if(is_file($file)){
//$file_type = mime_content_type($file);
$size = filesize($file);
$ext = $file->guessExtension();
//Do some stuff
代码工作正常,但我只是不知道如何在PHP端获取文件名。
答案 0 :(得分:0)
如果$ file-> guessExtension();运行正常,那么您应该能够通过以下方式获取文件名:$ file-> getClientOriginalName();在Other File Methods methods链接到UploadedFile类的Symfony documentation的过程中对此进行了记录