Please note that I want the compartment number to change.
<?php
$compartment = "1";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
<?php
$compartment = "2";
/* HERE I NEED SOME SCRIPT TO FIND THE EXTENSION OF THE FILE NAME $compartment AND TO SAVE THAT AS A VARIABLE NAMED 'EXTENSION'.*/
if (file_exists($$compartment.$extension)) {
echo "$compartment.$extension exists!
} else {
echo "No file name exists that is called $compartment. Regardless of extension."
}
?>
Thank You!
For finding a specific file type, simply use the 'type:' command, followed by the file extension. For example, you can find . docx files by searching 'type: . docx'.
Finding files by name is probably the most common use of the find command. To find a file by its name, use the -name option followed by the name of the file you are searching for.
If you'd like to always search within file contents for a specific folder, navigate to that folder in File Explorer and open the “Folder and Search Options.” On the “Search” tab, select the “Always search file names and contents” option.
You need glob()
.
$compartment = "2";
$files = glob("/path/to/files/$compartment.*"); // Will find 2.txt, 2.php, 2.gif
// Process through each file in the list
// and output its extension
if (count($files) > 0)
foreach ($files as $file)
{
$info = pathinfo($file);
echo "File found: extension ".$info["extension"]."<br>";
}
else
echo "No file name exists called $compartment. Regardless of extension."
by the way, what you are doing above is crying for a loop. Don' repeat your code blocks, but wrap one of them into this:
$compartments = array(1, 3, 6, 9); // or whichever compartments
// you wish to run through
foreach ($compartments as $compartment)
{
..... insert code here .......
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With