0

I have multiple file upload form and I get files name with php loop;

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.";}
    }

When the loop finishes, I want to create a string with combining uploaded files name and I will store on my database as a string;

I assume 3 files uploaded

$_FILES["upload"]["name"][0], $_FILES["upload"]["name"][1], $_FILES["upload"]["name"][2]

image1.jpeg,image2.jpeg,image3.jpeg

2
  • $string.=$_FILES["upload"]["name"][$i].','; in the loop Commented May 6, 2019 at 21:36
  • @tim thank you it works but there is a comma at the end of string. I remove that with this code; $string = rtrim($string, ","); Commented May 6, 2019 at 21:51

1 Answer 1

0

Push them onto an array, then call implode() at the end.

$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.";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.