Reputation: 3573
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>';
}
?>
Upvotes: 0
Views: 1959
Reputation: 639
There are several issues here.
Be careful about your comparison operator - you currently are reassigning the $files array in this line:
if ($files = array("gif", "jpeg", "jpg", "png");) {
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>';
}
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).
Upvotes: 3
Reputation: 1237
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.
Upvotes: 0
Reputation: 16828
$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>';
}
Upvotes: 1
Reputation: 5846
<?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>';
}
?>
Upvotes: 1
Reputation: 726
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>';
}
?>
Upvotes: 0
Reputation: 7643
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 ...';
}
Upvotes: 0