Laravel 中的 UploadedFile 和 File

UploadedFile

Laravel通过 Input::file 可获取上传的多张图片,返回类型是 Symfony\Component\HttpFoundation\File\UploadedFile 数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$files = \Input::file('goods_pics');
print_r($files);

Array
(
[0] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => test1.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 118499
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => C:\Users\yezhou\AppData\Local\Temp\phpBE09.tmp
[fileName:SplFileInfo:private] => phpBE09.tmp
)

[1] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => test2.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 118499
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => C:\Users\yezhou\AppData\Local\Temp\phpBE0A.tmp
[fileName:SplFileInfo:private] => phpBE0A.tmp
)

)

Symfony\Component\HttpFoundation\File\UploadedFile API:http://api.symfony.com/master/Symfony/Component/HttpFoundation/File/UploadedFile.html

使用示例:

1
2
3
4
5
6
7
foreach ($files as $file) {
if($file -> isValid()) {
echo $file->getClientMimeType(); //获取原始文件Mime类型
echo $file->getClientOriginalName(); //获取原始文件名称
echo $file->getPathname(); //获取文件路径
}
}

File

执行 UploadedFile 的 move 方法,即可得到 Symfony\Component\HttpFoundation\File\File 对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
$realFile = $file->move($destPath, $file->getClientOriginalName());
print_r($realFile);

Symfony\Component\HttpFoundation\File\File Object
(
[pathName:SplFileInfo:private] => C:\Server\www\restserver\public\tmp\test1.jpg
[fileName:SplFileInfo:private] => test1.jpg
)
Symfony\Component\HttpFoundation\File\File Object
(
[pathName:SplFileInfo:private] => C:\Server\www\restserver\public\tmp\test2.jpg
[:SplFileInfo:private] => test2.jpg
)

Symfony\Component\HttpFoundation\File\File API:http://api.symfony.com/master/Symfony/Component/HttpFoundation/File/File.html

其继承自 SplFileInfo类,详见:http://php.net/SplFileInfo

使用示例:

1
2
3
4
echo $realFile->getMimeType(); //获取文件Mime类型。若出现问题:Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?),则 php.ini 开启 extension=php_fileinfo.dll
echo $realFile->getPathName(); //获取文件路径
echo $realFile->getFileName(); //获取文件名
echo $realFile->getType(); //获取文件类型

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :