Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅从目录中获取图像_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 仅从目录中获取图像

Php 仅从目录中获取图像,php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一段代码,可以从我想要的目录中获取所有文件。我想验证我是否只读取目录中的图像,而不是所有文件 $documents = []; $filesInFolder = \File::files('/var/www/test/storage/app/public/random_images'); foreach ($filesInFolder as $path) { $documents[] = pathinfo($path);

我有一段代码,可以从我想要的目录中获取所有文件。我想验证我是否只读取目录中的图像,而不是所有文件

$documents = [];
        $filesInFolder = \File::files('/var/www/test/storage/app/public/random_images');
        foreach ($filesInFolder as $path) {
            $documents[] = pathinfo($path);
        }
        return View::make('documents.index')->with('documents', $documents);
pathinfo获取一个

    array:8 [▼
  0 => array:4 [▼
    "dirname" => "/var/www/test/storage/app/public/random_images"
    "basename" => "sjsdfoltroigj.jpg"
    "extension" => "jpg"
    "filename" => "sjsdfoltroigj"
  ]
  1 => array:4 [▼
    "dirname" => "/var/www/test/storage/app/public/random_images"
    "basename" => "local-economy4pdf-pasay-city.jpg"
    "extension" => "jpg"
    "filename" => "local-economy4pdf-pasay-city"
  ]

如何添加检查数组中所有扩展名的循环?

创建一个有效图像扩展名数组,如果路径信息与其中任何一个匹配,则将其添加到
$documents

$imageExtensions = ['jpg','png','gif']; //etc

$files = pathinfo($path);

foreach($files as $file) {

  if(in_array($file['extension'], $imageExtensions) {
    $documents[] = $pathInfo;
  }

}

创建一个有效图像扩展名数组,如果路径信息与其中任何一个匹配,则将其添加到
$documents

$imageExtensions = ['jpg','png','gif']; //etc

$files = pathinfo($path);

foreach($files as $file) {

  if(in_array($file['extension'], $imageExtensions) {
    $documents[] = $pathInfo;
  }

}

我不相信文件扩展名,而是使用它的MIME类型

使用函数,可以从文件中提取mime类型

使用,可以抓取所有MIME类型的图像,并99%确保您将获得目录中的所有图像

遵循以下代码及其注释:

// First, recover a list of mime types
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // risk of security, see: https://stackoverflow.com/a/26641565/1644937
$mimeTypes = json_decode(curl_exec($curl), true);
curl_close($curl);

// Second, get only the mime types for images (the ones that start with 'image' word)
$imagesMimeTypes = array_filter($mimeTypes, function ($key) {
    return strpos($key, 'image') === 0;
}, ARRAY_FILTER_USE_KEY);

// Third, loop through your files and check if they are images
$dir    = '/var/www/test/storage/app/public/random_images';
$files = scandir($dir);
foreach ($files as $file) {
    $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
    $fileMimeType = finfo_file($fileInfo, $dir . '\\' . $file);
    finfo_close($fileInfo);
    if (!array_key_exists($fileMimeType, $imagesMimeTypes)) {
        continue;
    }

    // The file will probably a image at this point
    echo $file;
    echo '<br>';
}
//首先,恢复mime类型列表
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//安全风险,见:https://stackoverflow.com/a/26641565/1644937
$mimeTypes=json_decode(curl_exec($curl),true);
curl_close($curl);
//其次,只获取图像的mime类型(以“image”字开头的类型)
$imagesMimeTypes=数组\过滤器($mimeTypes,函数($key){
返回strpos($key,'image')==0;
},数组\过滤器\使用\键);
//第三,循环浏览文件并检查它们是否是图像
$dir='/var/www/test/storage/app/public/random_images';
$files=scandir($dir);
foreach($files作为$file){
$fileInfo=finfo\u open(fileInfo\u MIME\u类型);
$fileMimeType=finfo\u file($fileInfo,$dir.'\\'.$file);
finfo_close($fileInfo);
如果(!array_key_存在($fileMimeType,$imagesimetypes)){
继续;
}
//此时,该文件可能会显示一个图像
echo$文件;
回声“
”; }
我不相信文件扩展名,而是使用它的MIME类型

使用函数,可以从文件中提取mime类型

使用,可以抓取所有MIME类型的图像,并99%确保您将获得目录中的所有图像

遵循以下代码及其注释:

// First, recover a list of mime types
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // risk of security, see: https://stackoverflow.com/a/26641565/1644937
$mimeTypes = json_decode(curl_exec($curl), true);
curl_close($curl);

// Second, get only the mime types for images (the ones that start with 'image' word)
$imagesMimeTypes = array_filter($mimeTypes, function ($key) {
    return strpos($key, 'image') === 0;
}, ARRAY_FILTER_USE_KEY);

// Third, loop through your files and check if they are images
$dir    = '/var/www/test/storage/app/public/random_images';
$files = scandir($dir);
foreach ($files as $file) {
    $fileInfo = finfo_open(FILEINFO_MIME_TYPE);
    $fileMimeType = finfo_file($fileInfo, $dir . '\\' . $file);
    finfo_close($fileInfo);
    if (!array_key_exists($fileMimeType, $imagesMimeTypes)) {
        continue;
    }

    // The file will probably a image at this point
    echo $file;
    echo '<br>';
}
//首先,恢复mime类型列表
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,'https://cdn.jsdelivr.net/gh/jshttp/mime-db@master/db.json');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);//安全风险,见:https://stackoverflow.com/a/26641565/1644937
$mimeTypes=json_decode(curl_exec($curl),true);
curl_close($curl);
//其次,只获取图像的mime类型(以“image”字开头的类型)
$imagesMimeTypes=数组\过滤器($mimeTypes,函数($key){
返回strpos($key,'image')==0;
},数组\过滤器\使用\键);
//第三,循环浏览文件并检查它们是否是图像
$dir='/var/www/test/storage/app/public/random_images';
$files=scandir($dir);
foreach($files作为$file){
$fileInfo=finfo\u open(fileInfo\u MIME\u类型);
$fileMimeType=finfo\u file($fileInfo,$dir.'\\'.$file);
finfo_close($fileInfo);
如果(!array_key_存在($fileMimeType,$imagesimetypes)){
继续;
}
//此时,该文件可能会显示一个图像
echo$文件;
回声“
”; }
我想您可以查一下分机。放置一个循环并只添加图像扩展名文件,我想您可以使用扩展名进行检查。放置一个循环并仅添加仅检查数组中一项而不是所有数组的图像扩展文件仅检查数组中一项而不是所有数组