php使用第一个字符获取扩展名为的文件名



嗯,这是我的第一个严肃的网络项目,在那一刻,我有一个巨大的谜。

交易是使用第一个字母获得上一个和下一个文件扩展名,例如:

有一个文件04.jpeg 05.gif 06.tiff,我们目前正在读取05.gif,所以我们删除了扩展名

现在是'05',所以我们加一个,所以我们得到'06',以及如何获得该文件扩展名

详细信息:

问题是,我需要修复我的图像浏览器,它在两种模式下工作:

  1. 漫画浏览器

    [文件夹中的漫画看起来像:

    page00.分机

    page01.分机

    (…)

    第09页分机

    第10页扩展

    第11页分机]

  2. 单文件浏览器

    [文件夹中的单个文件看起来像:

    01.扩展

    02.扩展

    03.扩展

    (…)

    09.扩展

    10.扩展

    11.延期]

[扩展名可能是jpg、jpeg、gif、png等]

我使用$_GET var.获取当前的$扩展名和文件名

以下是一些使用bootstrap 3框架的代码:

我也很抱歉没有评论,但这并不是很复杂,因为它看起来像

<?php
if($type == 2) {
$number = substr($clean, 4);
$nextnum = $number + 1;
$prevnum = $number - 1;

$prevpath = HOW TO GET IT WITH EXTENSION??
$nextpath = HOW TO GET IT WITH EXTENSION??
$nextext = '.' . substr(strrchr($prevpath,'.'),1);
$prevext = '.' . substr(strrchr($nextpath,'.'),1);

if ($nextnum >= 10) {
$next    = 'page' . $nextnum . $nextext;
} else {
$next    = 'page0' . $nextnum . $nextext; }
if ($prevnum >= 10) {
$prev    = 'page' . $prevnum . $prevext;
} else {
$prev    = 'page0' . $prevnum . $prevext; }
if ($clean === "page00") {
$prev = 'page00' . $extension; }
$count = 'page' . $x;
if ($clean === $count) {
$next = $count . $extension; }
} elseif($type == 3) {
$nextnum = $clean + 1;
$prevnum = $clean - 1;
$prevpath = HOW TO GET IT WITH EXTENSION?? <====== MY PROBLEM
$nextpath = HOW TO GET IT WITH EXTENSION?? <====== MY PROBLEM
$nextext = '.' . substr(strrchr($prevpath,'.'),1);
$prevext = '.' . substr(strrchr($nextpath,'.'),1);
if ($nextnum >= 10) {
$next = $nextnum . $nextext;
$prev = $prevnum . $prevext;
} else {
$next = '0' . $nextnum . $nextext;
$prev = '0' . $prevnum . $prevext; }
if ($clean === "01") {
$prev = '01' . $extension; }
if ($clean === $x) {
$next = $x . $extension; } }

echo '<br />
<div class="btn-group btn-group-justified">
<a class="btn btn-default" href="?browse=' . $prev . '" role="button">Poprzedni</a>
<a class="btn btn-default" href="' . $goback . '" role="button">Wyjdź</a>
<a class="btn btn-default" href="?browse=' . $next . '" role="button">Następny</a>
</div>';
?>
function get_next_file($file){
$maxdigits = 2; // assuming the file always ends with two digits
$dir = dirname($file) . DIRECTORY_SEPARATOR;
$fname = pathinfo($file, PATHINFO_FILENAME);
$name = substr($fname, 0, -$maxdigits);
$digits = substr($fname, -$maxdigits) + 1;
foreach(array('bmp', 'png', 'jpg', 'gif') as $ext){
$test = $dir . $name . str_pad($digits, $maxdigits, '0', STR_PAD_LEFT) . '.' . $ext;
if(file_exists($ext))return $ext;
}
return false; // no such file...
}

完整的功能看起来像上面的功能(未测试!)。你可以这样称呼它:

假设我们有这些文件:

test01.png
test02.gif
test05.bmp

你会得到以下结果:

get_next_file('test01.png') => 'test02.gif'
get_next_file('test02.gif') => false        // 'coz the other file is not consecutive
get_next_file('test05.bmp') => false        // no other files to look for...

如果您知道当前文件的名称,则可以使用glob:搜索目录中的文件

if (is_array(glob('05.*'))) { /* do some stuff */ }

否则,您可以创建一个whilte扩展列表并使用file_exists:

foreach (array('jpg', 'jpeg', 'png') as $ext)
if (file_exists('05.'. $ext)) { /* do some stuff */ }

相关内容

最新更新