ServerSideSkittles
ServerSideSkittles

Reputation: 2973

Returning each file path into an array?

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?

Upvotes: 0

Views: 70

Answers (1)

user1669496
user1669496

Reputation: 33058

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.

Upvotes: 1

Related Questions