laravel file上传文件信息获取

该文章详细整理了laravel常用上传文件信息获取的方法,记录一下,可以拿来就用!!!

1、获取上传的文件

//use Illuminate\Http\Request;
$file=$request->file('file');

2、获取上传文件的文件名(带后缀,如 abc.png)

$filename=$file->getClientOriginalName();

3、获取上传文件的后缀(如 abc.png,获取到的为 png)

$fileextension=$file->getClientOriginalExtension();

4、获取上传文件的大小

$filesize=$file->getClientSize();

6、获取文件的mime类型

$mimeTye = file -> getMimeType(); 

常用mime类型

$mime_types = array(

'gif' => 'image/gif',

'jpg' => 'image/jpeg',

'jpeg' => 'image/jpeg',

'jpe' => 'image/jpeg',

'bmp' => 'image/bmp',

'png' => 'image/png',

'tif' => 'image/tiff',

'tiff' => 'image/tiff',

'pict' => 'image/x-pict',

'pic' => 'image/x-pict',

'pct' => 'image/x-pict',

'tif' => 'image/tiff',

'tiff' => 'image/tiff',

'psd' => 'image/x-photoshop',

'swf' => 'application/x-shockwave-flash',

'js' => 'application/x-javascript',

'pdf' => 'application/pdf',

'ps' => 'application/postscript',

'eps' => 'application/postscript',

'ai' => 'application/postscript',

'wmf' => 'application/x-msmetafile',

'css' => 'text/css',

'htm' => 'text/html',

'html' => 'text/html',

'txt' => 'text/plain',

'xml' => 'text/xml',

'wml' => 'text/wml',

'wbmp' => 'image/vnd.wap.wbmp',

'mid' => 'audio/midi',

'wav' => 'audio/wav',

'mp3' => 'audio/mpeg',

'mp2' => 'audio/mpeg',

'avi' => 'video/x-msvideo',

'mpeg' => 'video/mpeg',

'mpg' => 'video/mpeg',

'qt' => 'video/quicktime',

'mov' => 'video/quicktime',

'lha' => 'application/x-lha',

'lzh' => 'application/x-lha',

'z' => 'application/x-compress',

'gtar' => 'application/x-gtar',

'gz' => 'application/x-gzip',

'gzip' => 'application/x-gzip',

'tgz' => 'application/x-gzip',

'tar' => 'application/x-tar',

'bz2' => 'application/bzip2',

'zip' => 'application/zip',

'arj' => 'application/x-arj',

'rar' => 'application/x-rar-compressed',

'hqx' => 'application/mac-binhex40',

'sit' => 'application/x-stuffit',

'bin' => 'application/x-macbinary',

'uu' => 'text/x-uuencode',

'uue' => 'text/x-uuencode',

'latex'=> 'application/x-latex',

'ltx' => 'application/x-latex',

'tcl' => 'application/x-tcl',

'pgp' => 'application/pgp',

'asc' => 'application/pgp',

'exe' => 'application/x-msdownload',

'doc' => 'application/msword',

'rtf' => 'application/rtf',

'xls' => 'application/vnd.ms-excel',

'ppt' => 'application/vnd.ms-powerpoint',

'mdb' => 'application/x-msaccess',

'wri' => 'application/x-mswrite',

);

7、获取缓存在 tmp 目录下的文件名(带后缀,如 php8933.tmp)

$filaname=$file->getFilename();

8、获取上传的文件缓存在 tmp 文件夹下的绝对路径

$realpath=$file->getRealPath();

9、将缓存在 tmp 目录下的文件移到某个位置,返回的是这个文件移动过后的路径

$path =base_path()."/uploads";
$newName = date('YmdHis').mt_rand(100,999).".".$extension;
$path=$file->move($path,$newName);

move () 方法有两个参数, $path 参数是文件移到哪个文件夹下的路径, $newName 参数是将上传的文件重新命名的文件名

10、检测上传的文件是否合法,返回值为 true 或 false

$file->isValid();

0 评论
内联反馈
查看所有评论