我要做的是检查文件的文件扩展名,如果文件的扩展名与扩展数组的任何元素匹配,那么我调用函数get_size()否则我显示错误文件不匹配...... 问题是扩展是否匹配我的代码的else语句是否被执行但是我只想在扩展名不匹配时执行else语句下面是我的代码
// getting the extension of the file
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
// allowed extension array
$extensions= array('txt','xls','xlsm','xlsx','xlsm','csv' );
// foreach loop for checking if extensions matches or not
foreach ($extensions as $extres)
{
# code...
if($extres===$ext)
{
get_size();
}
else
{
# code...
$once=1;
}
}
if($once==1)
{
exit("Sorry file not matched");
}
答案 0 :(得分:0)
使用以下代码:
// getting the extension of the file
$ext = pathinfo($file_name, PATHINFO_EXTENSIO-N);
// allowed extension array
$extensions= array('txt','xls','xlsm','xlsx','xlsm','csv' );
// foreach loop for checking if extensions matches or not
$once = 1;
foreach ($extensions as $extres)
{
# code...
if($extres===$ext)
{
get_size();
$once = 0;
}
else
{
# code...
//$once=1; //Remove the else part.
}
}
if($once==1)
{
exit("Sorry file not matched");
}