PHP循环合并数组第一个值并创建新字符串

时间:2019-05-06 21:33:24

标签: php

我有多个文件上传表单,并通过php循环获取文件名;

for( $i=0 ; $i < $total ; $i++ ) {
$target_file = $target_dir . basename($_FILES["upload"]["name"][$i]);
if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $target_file)) {
   echo "The file ". basename( $_FILES["upload"]["name"][$i]). " has been uploaded.";}
    }

循环结束后,我想结合上载的文件名创建一个字符串,并将其作为字符串存储在数据库中;

我假设已上传3个文件

$ _ FILES [“上传”] [“名称”] [0],$ _FILES [“上传”] [“名称”] [1],$ _FILES [“上传”] [“名称”] [2]

image1.jpeg,image2.jpeg,image3.jpeg

1 个答案:

答案 0 :(得分:0)

将它们放入数组,然后最后调用implode()

$filenames = [];
for( $i=0 ; $i < $total ; $i++ ) {
    $target_file = $target_dir . basename($_FILES["upload"]["name"][$i]);
    if (move_uploaded_file($_FILES["upload"]["tmp_name"][$i], $target_file)) {
        $filenames[] = basename($_FILES["upload"]["name"][$i]);
    }
}
echo "The files " . implode(", ", $filenames) . " have been uploaded.";