所以我有7个输入文件,我想在PHP中使用value
检索每个值。以下是代码:
$_FILES
但我认为这不是一种有效的方法。
此外,我想爆炸名称以获取文件的扩展名,如下所示:
$fig1 = $_FILES['files1']['name'];
$fig2 = $_FILES['files2']['name'];
$fig3 = $_FILES['files3']['name'];
$fig4 = $_FILES['files4']['name'];
$fig5 = $_FILES['files5']['name'];
$fig6 = $_FILES['files6']['name'];
$fig7 = $_FILES['files7']['name'];
我还有6个变量。
那么,还有更有效的方法吗?
答案 0 :(得分:0)
使用此
$ext = array();
foreach($_FILES as $key=>$val)
{
$value_fig = explode('.', $_FILES[$key]['name']);
$ext [] = strtolower(array_pop($value_fig));
}
$ ext获取扩展数组
答案 1 :(得分:0)
您可以这样做:
$i = 0;
foreach($_FILES as $file)
{
$tmp = explode('.', $file['name']);
${'file_ext_fig'.++$i} = strtolower(array_pop($tmp));
${'file_name_fig'.$i} = implode('.', $tmp);
}
现在您拥有以下内容:$file_ext_fig1
,$file_name_fig1
,$file_ext_fig2
,$file_name_fig2
等等...
顺便说一句,我必须告诉你,你不应该使用这种方法来确定文件扩展名。如上所述:https://stackoverflow.com/a/10368236/3799829您应该执行以下操作
$i = 0;
foreach($_FILES as $file)
{
${'file_ext_fig'.++$i} = pathinfo($file['name'], PATHINFO_EXTENSION);
${'file_name_fig'.$i} = pathinfo($file['name'], PATHINFO_FILENAME);
}
顺便说一下,你会看到我正在使用++$i
,如果这让你感到不安,你可以这样做:
$i = 0;
foreach($_FILES as $file)
{
$i++;
${'file_ext_fig'.$i} = pathinfo($file['name'], PATHINFO_EXTENSION);
${'file_name_fig'.$i} = pathinfo($file['name'], PATHINFO_FILENAME);
}
完全相同
如果您要过滤要使用的文件字段,请执行以下操作
// the fields you want to process
$filter = array(
'fig1_field_name',
'fig2_field_name',
'fig3_field_name',
'fig4_field_name',
'fig5_field_name',
);
$i = 0;
foreach($_FILES as $key => $file)
{
if(!in_array($key, $filter)) continue;
$i++;
${'file_ext_fig'.$i} = pathinfo($file['name'], PATHINFO_EXTENSION);
${'file_name_fig'.$i} = pathinfo($file['name'], PATHINFO_FILENAME);
}