I have a value at $field
$field is a 5 digit number, and I have a folder full of images in image sets. So for each number e.g. "12345", there could be "12345_1" or "12345_2" etc. if multiple images exist.
I am using the following to check if each file exists, and then build a comma separated value for the files that do exist to insert into a CSV file.
For example, if A exists, and B exists, return "A,B"
I know what I am trying to achieve, and have the following...
if (file_exists('images/' . $field . '.JPG')) {
$file1 = 'images/' . $field . '.JPG';
}
if (file_exists('images/' . $field . '_1.JPG')) {
$file2 = 'images/' . $field . '_1.JPG';
}
if (file_exists('images/' . $field . '_2.JPG')) {
$file3 = 'images/' . $field . '_2.JPG';
}
if((isset($file1)) && (!isset($file2)) && (!isset($file3))) {
return "$file1";
} elseif((isset($file1)) && (isset($file2)) && (!isset($file3))) {
return "$file1,$file2";
} elseif((isset($file1)) && (isset($file2)) && (isset($file3))) {
return "$file1,$file2,$file3";
}
But this just seems like a really crap way of writing what I am trying to do, especially as in the real example, I am doing this for up to 10 files, so this is quite bulky.
Is there a better way to do this, or a method I am missing out on that could make life easier? (I'm still learning!)
ifinside the firstifstatement.forloop, then you already have your numbers.