2

Huh, this is my first serious web project and for that moment i've got simply big enigma.

The deal is to get previous and next file extension using its first letters, for instance:

there's file 04.jpeg 05.gif 06.tiff we're currently reading 05.gif so we remove extension

and now its '05' so we add one so we get '06' and HOW TO GET THAT FILE EXTENSION?!?!?!

Details:

The problem is that i need to fix my image browser which works in two modes:

  1. Comics browser

    [comics in folder looks like:

    page00.extension

    page01.extension

    (...)

    page09.extension

    page10.extension

    page11.extension ]

  2. Single files browser

    [single files in folder looks like:

    01.extension

    02.extension

    03.extension

    (...)

    09.extension

    10.extension

    11.extension ]

[extension maybe jpg,jpeg,gif,png etc.]

Im getting current $extension and file name using $_GET var.

Here is some code - im working with bootstrap 3 framework:

Also im sorry that there's no comments but this isn't really complicated as it looks like

<?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>';

?>
3
  • Why not just convert all the images to one format? Commented Mar 2, 2014 at 16:29
  • because some of them are gifs some are not. i have to leave extension as it is. Commented Mar 2, 2014 at 16:34
  • 1
    you can take a look at scandir Commented Mar 2, 2014 at 16:37

2 Answers 2

3
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...
}

The complete function would look like the one above (not tested!). You would call it like so:

Assuming we have these files:

test01.png
test02.gif
test05.bmp

You get these results:

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...
Sign up to request clarification or add additional context in comments.

Comments

3

If you know the name of current file then you can search for files in a directory using glob:

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

Else you can create a whilte-list of extensions and use file_exists:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.