0

I have a script right now that works great for a FTP type site for my company resource.

It displays images with a download link and file name. What I want to do is, if the file isn't an image (like pdf) show a placeholder image.

The code below isn't right (the if/else statement), but you can see what I was going for.

Thanks in advance.

<?php

        // Find all files in that folder
        $files = glob('files/*');
        //$image = 

        // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
        natcasesort($files);

        if ($files = array("gif", "jpeg", "jpg", "png");) {
            $images = $file
        } else {
            $images = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
        }


        // Display images
        foreach($files as $file) {
           echo '<div class="one_half"><img src="' . $images . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="fitter/download.php?file='.base64_encode($file).'"></a></div>';
        }

    ?>
1
  • can u give dump output of $files object ? Commented May 31, 2013 at 18:52

6 Answers 6

3

There are several issues here.

  1. Be careful about your comparison operator - you currently are reassigning the $files array in this line:

    if ($files = array("gif", "jpeg", "jpg", "png");) {
    
  2. You are comparing your $files array to another array in a meaningless way. You could potentially build out a separate $images array, as it looks like you're trying to do, but this would be better accomplished within the foreach loop, as in:

    foreach($files as $file) {
      $filepath = pathinfo($file);
      if (in_array($filepath['extension'], ('gif', 'jpeg', 'jpg', 'png')) {
        $image = $file;
      } else {
        $image = 'http://hg.exbabylon.net/find_fitter/placeholder.jpg';
      }
    
      echo '<div class="one_half"><img src="' . $images . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="fitter/download.php?file='.base64_encode($file).'"></a></div>';
    }
    
  3. Ideally, you should be looking at mime type rather than file extension. In PHP 5.3, this is done as follows:

    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
    $mimetype = finfo_file($finfo, $file);
    finfo_close($finfo);
    

You can then do a comparison based on the mime type as in (2).

Sign up to request clarification or add additional context in comments.

Comments

1
$allowed_extensions = array("gif", "jpeg", "jpg", "png");
$files = glob('files/*');

natcasesort($files);
foreach($files as $file){
    if(!in_array(end(explode(".",$file)),$allowed_extensions)){
        $image = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
    }else{
        $image = $file;
    }
    echo '<div class="one_half"><img src="' . $image . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="fitter/download.php?file='.base64_encode($file).'"></a></div>';
}

Comments

1
<?php
    $allowed_extensions = array("gif", "jpeg", "jpg", "png");

    // use values as keys for lasier lookups
    $allowed_extensions = array_combine($allowed_extensions, $allowed_extensions);

    // Find all files in that folder
    $files = glob('files/*');

    // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
    natcasesort($files);

    // Display images
    foreach($files as $file) {

       // split file name at "." and use last part as file extension
       $file_parts = explode('.', $file);
       $file_extension = array_pop($file_parts)

       // check if file extensions is allowed
       if($allowed_extensions[$file_extension]) {
           $images = $file;
       } else {
           $images = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
       }

       echo '<div class="one_half"><img src="' . $images . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="fitter/download.php?file='.base64_encode($file).'"></a></div>';
    }

?>

1 Comment

for allowing ".JpG" and other case combination you can use: $file_extension = strtolower(array_pop($file_parts))
0

Assuming that your $files holds just a file name + extension i.e. myimage.png

foreach($files as $file) {
   $split = explode(".", $file);
   $extension = array_pop($split);

   if (in_array($extension, array('gif', 'jpeg', 'jpg', 'png'))) {
      $images = $file;
   } else {
      $images = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
   }

   //display your image
   echo '<div ...';
}

Comments

0

Change it into something like this

<?php

        // Find all files in that folder
        $files = glob('files/*');

        // Do a natural case insensitive sort, usually 1.jpg and 10.jpg would come next to each other with a regular sort
        natcasesort($files);

        $extensions = array("gif", "jpeg", "jpg", "png");

        // Display images
        foreach($files as $file) {
           $pi - pathinfo($file);
           if(!in_array($pi['extension'], $extensions)) {
              $file = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
           }

           echo '<div class="one_half"><img src="' . $file . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="fitter/download.php?file='.base64_encode($file).'"></a></div>';
        }

    ?>

2 Comments

"$extensions = $files = array("gif", "jpeg", "jpg", "png");" whould damage the list of files
My bad, it was a typo. I changed it
0

You can use regular expressions and the preg_match function:

if (preg_match("/.*\.gif|jpg|jpeg|png$/i", $files)) {
  $images = $file
} else {
  $images = "http://hg.exbabylon.net/find_fitter/placeholder.jpg";
}

Also, see preg_match documentation.

Note: it would be better to check the mime-type (if possible), because filename can be easily changed.

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.