gpt4 book ai didi

php - 查找具有任何扩展名的特定名称的文件

转载 作者:可可西里 更新时间:2023-11-01 12:25:34 27 4
gpt4 key购买 nike

请注意,我要更改车厢号。

<?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."
}
?>

谢谢!

最佳答案

你需要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."

顺便说一句,你上面做的是在哭一个循环。不要重复你的代码块,而是将其中一个包装成这样:

 $compartments = array(1, 3, 6, 9); // or whichever compartments 
// you wish to run through

foreach ($compartments as $compartment)
{
..... insert code here .......
}

关于php - 查找具有任何扩展名的特定名称的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3202203/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com