我在PHP中有一个findtexts函数来获取上传文件的扩展名,但它无效。如果我回复$ ext,我什么也得不到。
if (isset($_POST['submit']))
{
if ($_POST['mainfile'] != "")
{
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = preg_split("%[/\\\\.]%", $filename);
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['mainfile']['name']) ;
$ran = rand () ;
$ran2 = $ran.".";
$target = $pid."_main_".$ran2.$ext;
$ok=1;
echo "ext= " .$ext;
echo "<br>target = " .$target;
}
}
答案 0 :(得分:0)
试试这个
if (isset($_POST['submit']))
{
if (isset($_FILES['mainfile']))
{
function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = preg_split("%[/\\\\.]%", $filename);
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
//This applies the function to our file
$ext = findexts ($_FILES['mainfile']['name']) ;
$ran = rand () ;
$ran2 = $ran.".";
$target = $pid."_main_".$ran2.$ext;
$ok=1;
echo "ext= " .$ext;
echo "<br>target = " .$target;
}
}
答案 1 :(得分:0)
使用以下方法从文件中提取扩展名。
pathinfo($filename, PATHINFO_EXTENSION)