在Laravel中获取远程文件的mime类型

时间:2016-03-15 18:00:02

标签: php laravel

我使用file_get_contents从远程服务器获取图像。在我做任何事情之前,我需要检查文件是否是jpeg。

尝试:

$file = file_get_contents('http://server.com/images/new.jpg');
$mimetype = Storage::mimeType(basename($file));
echo $mimetype;

但所有这些都是directory

如何从远程文件中获取mimetype?

1 个答案:

答案 0 :(得分:2)

你可以通过调用头部请求来获取它:

$headers=get_headers("http://server.com/images/new.jpg");

这将返回头数据,您可以从那里获取内容类型。

dd($headers);并获取您想要的数据

例如:

$headers=get_headers("https://d13yacurqjgara.cloudfront.net/users/1039396/screenshots/2584581/4_1x.png");
dd($headers);

返回:

array:16 [▼
  0 => "HTTP/1.1 200 OK"
  1 => "Content-Type: image/png"
  2 => "Content-Length: 59913"
  3 => "Connection: close"
  4 => "Date: Tue, 15 Mar 2016 18:04:38 GMT"
  5 => "x-amz-version-id: 7G..R3PteLad69jfLdbqN1FcyPDt2cGf"
  6 => "Expires: Thu, 12 Mar 2026 00:00:15 GMT"
  7 => "Cache-Control: max-age=315576000"
  8 => "Last-Modified: Sat, 12 Mar 2016 02:38:29 GMT"
  9 => "ETag: "f525b704270fa860869ce186fcf12de8""
  10 => "Accept-Ranges: bytes"
  11 => "Server: AmazonS3"
  12 => "Age: 32"
  13 => "X-Cache: Hit from cloudfront"
  14 => "Via: 1.1 5ec64d9502b4a51a283c9c8c16414219.cloudfront.net (CloudFront)"
  15 => "X-Amz-Cf-Id: ITHcrpnzBuzGuJsEXgI2uZ4YGx3iA_LoAAx36y4ghHDrOxo2VVRXSA=="
]