Completed
Push — master ( 4dd6d0...6c40dd )
by Taosikai
10:58
created

docs/samples/advanced.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
include __DIR__ . '/bootstrap.php';
3
4
use Slince\Upload\UploadHandlerBuilder;
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
7
$builder = new UploadHandlerBuilder();
8
$handler = $builder
9
    //Custom namer
10
    ->naming(function (UploadedFile $file) {
11
        return date('Y/md') . '/' . uniqid() . '.' . $file->getClientOriginalExtension();
12
    })
13
14
    //add constraints
15
    ->sizeBetween('10m', '20m')
16
    ->allowExtensions(['jpg', 'txt'])
17
    ->allowMimeTypes(['image/*', 'text/plain'])
18
19
    ->saveTo(__DIR__ . '/dst')
20
    ->getHandler();
21
22
$files = $handler->handle();
23
24
foreach ($files as $file) {
0 ignored issues
show
The expression $files of type array<integer,object<Sym...tion\File\UploadedFile> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
25
    if ($file instanceof \Exception) {
26
        echo 'upload error: ' . $file->getMessage(), PHP_EOL;
27
    } else {
28
        echo 'upload ok, path:' . $file->getPathname();
29
    }
30
}
31
32