Viewed   96 times

How can I upload multiple files in Laravel 5.3. If I try it with 1 image it works but multiple images are not uploaded.

This is my code:

if($request->hasFile('attachment')) {
    foreach($request->allFiles('attachments') as $file) {
        $file->store('users/' . $user->id . '/messages');
    }
}

 Answers

1

It works now like this:

$files = $request->file('attachment');

if($request->hasFile('attachment'))
{
    foreach ($files as $file) {
        $file->store('users/' . $this->user->id . '/messages');
    }
}

I had to append [] after the value of the name attribute, so:

<input type="file" name="attachment[]" multiple>
Sunday, August 14, 2022
 
3

Well, I'm not exactly sure what was causing the error, but after playing around a bit with the validator, I figured out that I couldn't just pass all the files at once. Instead, I made a new array that associated each file with keys 'file0', 'file1', etc. Then I passed these to the validator and set rules for each one (using a foreach loop). Then the validator worked as expected. Still, it wasn't flexible enough for my needs and I ended up using a non-laravel solution.

Sunday, October 16, 2022
 
5

Try removing the blank $messages array and calling the function all() for the inputs on the request.

$rules = [
    'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
];
$this->validate($request,$rules);

To display the default error message you would throw an exception using something like:

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
        return redirect('VIEWPATH.VIEWNAME')->withErrors($validator)->withInput();
    }

Then to display the errors you can have something like the following in your blade view or template:

@if (count($errors) > 0)
  <div class="alert alert-danger alert-dismissable fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <h4>
      <i class="icon fa fa-warning fa-fw" aria-hidden="true"></i>
      <strong>Error!</strong> See error messages...
    </h4>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

or

@if(session()->has('errors'))
    <div class="alert alert-danger fade in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4>Following errors occurred:</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Tuesday, December 27, 2022
 
3

Laravel 5.3 uses Storage instead of Files. You can access all of the files in a directory using either of these two methods:

use IlluminateSupportFacadesStorage;

$files = Storage::files($directory);

$files = Storage::allFiles($directory); // Includes subdirectories
Tuesday, August 9, 2022
1

You have to gain access over STDERR and, probably, STDOUT. Use proc_open, e.g.:

$desc = [
  1 => ['pipe', 'w'], // STDOUT
  2 => ['pipe', 'w'], // STDERR
];

$proc = proc_open('ls -l . something', $desc, $pipes);
if (is_resource($proc)) {

  if ($out = stream_get_contents($pipes[1])) {
    echo $out;
  }
  fclose($pipes[1]);


  if ($err = stream_get_contents($pipes[2])) {
    fprintf(STDERR, "Error: %sn", $err);
  }
  fclose($pipes[2]);

  // You can also check the process exit status
  // 0 means success, otherwise error.
  $exit_status = proc_close($proc);
}

Of course, there is no need in STDOUT pipe, if the command redirects it to a file.

And yes, system() won't throw exceptions. Obviously, you can implement your own class which will throw an exception in case if the process exit status is non-zero, or there is something caught in the STDERR pipe:

class MyShellException extends Exception {}

class MyShell {
  public static function execute($command, &$out = null) {
    if (func_num_args() > 1) {
      $desc[1] = ['pipe', 'w'];
    } else {
      $desc[1] = ['file', '/dev/null'];
    }

    $desc[2] = ['pipe', 'w'];

    $proc = proc_open($command, $desc, $pipes);
    if (is_resource($proc)) {
      if (isset($pipes[1])) {
        $out = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
      }

      if ($err = stream_get_contents($pipes[2])) {
        fclose($pipes[2]);
        throw new MyShellException("Command $command failed: $err");
      }

      if ($exit_status = proc_close($proc)) {
        throw new MyShellException("Command $command exited with non-zero status");
      }
    }
  }
}


try {
  MyShell::execute('ls -l . something', $out);
  echo "Output: $outn";
} catch (MyShellException $e) {
  if (!empty($out)) {
    echo "Output: $outn";
  }
  fprintf(STDERR, "MyShell error: " . $e->getMessage());
  exit(1);
}
Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :