basically this is like 60% written by me with a few copy/paste. What I am looking to do, is clean up this code a little. Functionally it does what I want it to relatively well, but it does look very very messy and I would like it to be a little more tidy.
Any suggestions on how to do this? I am no PHP expert as you can most likely tell by the code itself.
<?php
require(“cfg.php”);
//FOLDER PATH
$dir_cat= $imgpath;
$openDir_cat = opendir($dir_cat);
while (false !== ($fileNames_cat = readdir($openDir_cat))) {
$check_cat = $dir_cat. “/” . $fileNames_cat;
$size_cat = @getimagesize($check_cat);
// this check mime of file
if($fileNames == “.” || $fileNames_cat == “…” || strpos($size_cat[mime], “image”) === FALSE) {
continue; // exclude everything wha is not image
} else {
$imagesAll_cat = $fileNames_cat; // create an array of images
}
}
$imgnr_cat=0;
$imgct_cat=count(glob($dir_cat . “*”)); //COUNT THE NUMBER OF FILES IN THE UPLOAD DIR.
echo ‘<center><fieldset class=fieldset>’;
echo '<legend> IMGS ';
echo ’ (’ .$imgct_cat. ’ IMAGES)</legend><div>';
//GET IMAGES AND DISPLAY THEM
while ($imgnr_cat<$imgct_cat){
echo ‘<a href=’ .$dir_cat.$imagesAll_cat[$imgnr_cat]. ‘>’;
echo ‘<img width=140 height=100 src=’ .$dir_cat.$imagesAll_cat[$imgnr_cat]. ‘>’;
echo ‘</a>’;
$imgnr_cat++;
}
echo ‘</div></fieldset></center>’;
?>
This should work
<?php
require('cfg.php');
// Set the path to the folder
$dir = $imgpath;
$images = array();
// Make sure the directory exists
if (is_dir($dir)) {
// Create a new directory handle
if ($handle = opendir($dir_cat)) {
// Loop through all the files in the directory
while(false !== ($file = readdir($handle))) {
// Get some information about the file
$fileinfo = @getimagesize($dir_cat . '/' . $file);
// Check the file mime type
// Only append to the array if the file is an image
if (strpos($fileinfo['mime'], 'image')) {
$images[] = $file;
}
}
closedir($handle);
// List the images
if (sizeof($images) > 0) {
echo '<center><fieldset class="fieldset">';
echo ' <legend> IMGS (' . count($images). ' IMAGES)</legend>';
echo ' <div>';
foreach($images as $image) {
echo '<a href="' . $dir . $image . '"><img src="' . $dir . $image . '" width="140" height="100"></a>';
}
echo ' </div>';
echo '</fieldset></center>';
} else {
echo 'No images exist in the directory (' . $dir . ')';
}
} else {
echo 'An error has occurred while trying to open the directory (' . $dir . ')';
}
} else {
echo 'The directory (' . $dir . ') either doesn\\'t exist or cannot be opened due to insufficient permissions';
}
?>
There are numerous Google references to formatting coding style. Here is the one that I follow:
http://expressionengine.com/user_guide/development/guidelines/general.html
The code you supplied looks a lot better and easier to read if enclosed using the [B]"wrap
tags behind selected text"[/B]
```php
<?php
require("cfg.php");
//FOLDER PATH
$dir_cat= $imgpath;
$openDir_cat = opendir($dir_cat);
while (false !== ($fileNames_cat = readdir($openDir_cat)))
{
$check_cat = $dir_cat. "/" . $fileNames_cat;
$size_cat = @getimagesize($check_cat);
// this check mime of file
if($fileNames == "." || $fileNames_cat == ".." || strpos($size_cat[mime], "image") === FALSE)
{
continue; // exclude everything wha is not image
}else{
$imagesAll_cat[] = $fileNames_cat; // create an array of images
} endif
}//endwhile
$imgnr_cat=0;
$imgct_cat=count(glob($dir_cat . "*")); //COUNT THE NUMBER OF FILES IN THE UPLOAD DIR.
echo '<center><fieldset class=fieldset>';
echo '<legend> IMGS ';
echo ' (' .$imgct_cat. ' IMAGES)</legend><div>';
//GET IMAGES AND DISPLAY THEM
while ($imgnr_cat<$imgct_cat)
{
echo '<a href=' .$dir_cat.$imagesAll_cat[$imgnr_cat]. '>';
echo '<img width=140 height=100 src=' .$dir_cat.$imagesAll_cat[$imgnr_cat]. '>';
echo '</a>';
$imgnr_cat++;
}//endwhile
?>
</div></fieldset></center>
_______________