0

I have a upload function in my controller that takes multiple file uploads. So far it uploads the files to the directory but I want to capture each one into an array.

How would I go about doing this?

Here is the code

$files = Request::file('document');

        foreach($files as $file) {
            $rules = array(
               'file' => 'required|mimes:png,gif,jpeg,txt,pdf,doc,docx,rtf|max:20000'
            );
            $validator = \Validator::make(array('file'=> $file), $rules);
            if($validator->passes()){

                $destinationPath = '/public/uploads/';
                $mime_type = $file->getMimeType();
                $extension = $file->getClientOriginalExtension();
                $filename = str_random(12) . '.' . $extension;  
                $upload_success = $file->move($destinationPath, $filename);

            } else {
                return Redirect::back()->with('error', 'We only accept png,gif,jpeg,txt,pdf,doc,rtf.'); // add error here.
            }
        }

When validator passes it processes each one but when I add a variable like $path = $upload_success; it obviously just takes the last one processed. How can I capture an array of all the file paths processed into $path?

2
  • If $file->move() returns the final path to the file then: $paths[] = $upload_success;. No? Commented May 11, 2015 at 16:56
  • yes, $filename is actually the path I want. Commented May 11, 2015 at 17:08

1 Answer 1

1

Before the foreach, create an array $paths = [];

After the $upload_success var is created, add it to the array. $paths[] = $upload_success;

Now $paths will be an array of all your paths.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for the solution! I have a nice array to loop in another function now :)

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.