-2

I wrote a short php code that displays an image and the titke to every file in a directory. The problem is that if i run the code many files get the a instead of an image. So for example a .zip file is in front of a .txt file. Now it will display the of the txt file instead of the .zip image.

The code:

$file_dir = "upload/demofiles";
if ($handle = opendir($file_dir)) {
    $i = 1;
    while (false !== ($entry = readdir($handle))) {
    $format = pathinfo($entry, PATHINFO_EXTENSION);
    $file_path = $file_dir.'/'.$entry;

    if($format == "txt"){
        $myfile = fopen($file_path, "r") or die("Unable to open file!");
        $file_element = '<iframe src="'.$file_path.'" width="80px" style="height:80px" scrolling="no"></iframe>';
    }

    if($format == "png"){
        $myfile = fopen($file_path, "r") or die("Unable to open file!");
        $file_element = '<img src="'.$file_path.'" />';
    }

    if($format == "zip"){
        $myfile = fopen($file_path, "r") or die("Unable to open file!");
        $file_element = '<img width="64px" class="img" src="icons/zip.png" />';
        fclose($myfile);
    }

    if($format == "pdf"){
        $file_element = '<embed src="'.$file_path.'" width="80px" scrolling="no" style="height:80px">';
    }

    if(!isset($file_element)){
        $file_element = '<img class="img" width="64px" src="icons/file.png">';
    }

    if ($entry != "." && $entry != "..") {
        echo '<label style="display:inline-block; border:solid 1px;">
                <div>'.$file_element.'</div>
                <div>'.$entry.'</div>
        </label>';
    }
    $i++;
    }
}

why does this happend? I think the variables should be cleared after on loop run.

Thank you!

  • Why does what happen? `"The problem is that if i run the code many files get the a instead of an image"`...get the a?? – developerwjk Aug 28 '15 at 22:17

1 Answers1

0

Try this code:

$file_dir = "upload/demofiles";
if ($handle = opendir($file_dir)) {
    $i = 1;
    while (false !== ($entry = readdir($handle))) {
    $format = pathinfo($entry, PATHINFO_EXTENSION);
    $file_path = $file_dir.'/'.$entry;

    switch($format){
        case "txt":
            $myfile = fopen($file_path, "r") or die("Unable to open file!");
            $file_element = '<iframe src="'.$file_path.'" width="80px" style="height:80px" scrolling="no"></iframe>';
        break;

        case "png":
            $myfile = fopen($file_path, "r") or die("Unable to open file!");
            $file_element = '<img src="'.$file_path.'" />';
        break;

        case "zip":
            $myfile = fopen($file_path, "r") or die("Unable to open file!");
            $file_element = '<img width="64px" class="img" src="icons/zip.png" />';
            fclose($myfile);
        break;

        case "pdf":
            $file_element = '<embed src="'.$file_path.'" width="80px" scrolling="no" style="height:80px">';
        break;

        default:
            $file_element = '<img class="img" width="64px" src="icons/file.png">';
        break;
    }

    if ($entry != "." && $entry != "..") {
        echo '<label style="display:inline-block; border:solid 1px;">
                <div>'.$file_element.'</div>
                <div>'.$entry.'</div>
        </label>';
    }
    $i++;
    }
}
Ahmad
  • 5,551
  • 8
  • 41
  • 57