function addFlyer($db) {
if (isset($_FILES['file_array'])) {
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for ($i = 0; $i < count($tmp_name_array); $i++) {
if (file_exists($name_array[$i])) {
echo "Sorry file already exists. ";
$uploadOk = 0;
}
else {
if (move_uploaded_file($tmp_name_array[$i], "images/" . $name_array[$i])) {
echo $name_array[$i] . " upload is complete<br>";
} else {
echo "move_uploaded_file function failed for " . $name_array[$i] . "<br>";
}
}
}
}
目前,我得到了此代码,用于将多个文件上传到文件夹并将其插入数据库(我排除了要插入的代码。
但是,如果我想正确使用 file_exists 函数,我需要将扩展名和名称与文件分开。但我不知道如何爆炸一个数组。
有人可以帮助我吗?
你不需要爆炸或其他东西。您可以通过path_info((PHP函数获取图像扩展。请看下面。
$path = $_FILES['file_array']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
如果要分解文件名。您可以使用以下技巧
$file = $_FILES['file_array']['name'];
$image_info = explode(".", $file);
$image_type = end($image_info)
foreach ($name_array as $value) {
if (file_exists($value)) {
echo "Sorry file already exists. ";
$uploadOk = 0;
} else {
if (move_uploaded_file($value, "images/" . $value)) {
echo $value . " upload is complete<br>";
} else {
echo "move_uploaded_file function failed for " . $value . "<br>";
}
}
}
这可能会起作用。使用 foreach(( 循环而不是 for(( 循环。