Laravel 中的 UploadedFile 和 File
UploadedFile
Laravel通过 Input::file 可获取上传的多张图片,返回类型是 Symfony\Component\HttpFoundation\File\UploadedFile 数组。
$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
使用示例:
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 对象。
$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
使用示例:
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(); //获取文件类型
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/19/laravel-uploadedfile-file/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论