alut, je suis sur un projet et j'aimerais mettre sur pied un système d'upload de fichier, mais je rencontre un problème, le fichier n'est pas uploader
voici la méthode qui se charger d'envoyer les données en base de données
mon Entité
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 ****public function postUser(Request $request, Response $response, User $customer, Image $image, Router $router, Validator $validator) { ********$validation = $validator->validate($request, [ ************'email' => v::noWhitespace()->notEmpty()->email(), ************'name' => v::notEmpty()->alpha(), ************'url'* => v::noWhitespace(), ************'password' => v::noWhitespace()->notEmpty(), ************'role' => v::notEmpty()->alpha(), ********]); ********* ********if ($validation->fails()) { ************return $response->withRedirect($router->pathFor('add.customer')); ********}******* ********* ********$customer = $customer->create([ ************'email' => $request->getParam('email'), ************'name' =>* $request->getParam('name'), ************'password' =>* password_hash($request->getParam('password'), PASSWORD_DEFAULT), ************'role' =>* $request->getParam('role'), ************'image_id' => $image->id, ********]);******* ********$var = 'avatar'; ********$image = $image->firstOrCreate([ //*********** 'alt' => $request->getParam('alt'), ************* ************'url' =>* $image->upload($var), ********]); ********* ********$image->customers()->save($customer); ********* ********return $response->withRedirect($router->pathFor('home'));******* ****}
mon formulaire
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 <?php * namespace Cart\Models; * use Illuminate\Database\Eloquent\Model; use Cart\Models\Customer; use Upload\Storage\FileSystem; use Upload\File; use Upload\Validation\Mimetype; use Upload\Validation\Size; * /** ** Description of Image ** ** @author mbele **/ class Image extends Model{ ****protected $table = 'image'; ***** ****protected $fillable = [ ********'alt', ********'url', ****]; * ****public function customers(){ ********return $this->hasOne(Customer::class); ****}*** ***** ****public function upload() { ********$storage = new FileSystem('../public/img'); ********$file = new File('avatar', $storage); * ********// Optionally you can rename the file on upload ********$new_filename = uniqid(); ********$file->setName($new_filename); * ********// Validate file upload ********// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml ********$file->addValidations(array( ************// Ensure file is of type "image/png" ************new Mimetype('image/png', 'image/gif', 'image/jpg', 'image/pdf'), * ************//You can also add multi mimetype validation ************//new \Upload\Validation\Mimetype(array('image/png', 'image/gif')) * ************// Ensure file is no larger than 5M (use "B", "K", M", or "G") ************new Size('5M') ********)); * ********// Access data about the file that has been uploaded ********$data = array( ************'name'****** => $file->getNameWithExtension(), ************'extension'* => $file->getExtension(), ************'mime'****** => $file->getMimetype(), ************'size'****** => $file->getSize(), ************'md5'******* => $file->getMd5(), ************'dimensions' => $file->getDimensions() ********); * ********// Try to upload file ********try { ************// Success! ************$file->upload(); ********} catch (\Exception $e) { ************// Fail! ************$errors = $file->getErrors(); ********}******* ****} }
Type: InvalidArgumentException Message: Cannot find uploaded file identified by key: avatar File: C:\laragon\www\cart\vendor\codeguy\upload\src\Upload\File.php Line: 139 Trace
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 {% extends 'templates/app.twig' %} {% block content %} <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="panel panel-default"> <div class="panel-heading">Add customer</div> <div class="panel-body"> <form action="{{ path_for('add.customer') }}" method="post" autocomplete="off" enctype="multipart/form-data"> <div class="form-group{{ errors.email ? ' has-error' : '' }}"> <label for="email">Email</label> <input type="email" name="email" id="email" placeholder="you@domain.com" class="form-control"> {% if errors.email %} <span class="help-block">{{ errors.email | first }}</span> {% endif %} </div> <div class="form-group{{ errors.name ? ' has-error' : '' }}"> <label for="name">Name</label> <input type="text" name="name" id="name" class="form-control"> {% if errors.name %} <span class="help-block">{{ errors.name | first }}</span> {% endif %} </div> <div class="form-group{{ errors.file ? ' has-error' : '' }}"> <label for="avatar">Avatar</label> <input type="hidden" name="avatar" role="uploadcare-uploader"/> {# <input type="hidden" name="image" class="form-control" value=""/>#} {% if errors.file %} <span class="help-block">{{ errors.file | first }}</span> {% endif %} </div> <div class="form-group{{ errors.password ? ' has-error' : '' }}"> <label for="password">Password</label> <input type="password" name="password" id="password" class="form-control"> {% if errors.password %} <span class="help-block">{{ errors.password | first }}</span> {% endif %} </div> <input type="hidden" name="role" id="role" value="user" class="form-control"> <button type="submit" class="btn btn-default">Sign up</button> {{ csrf.field | raw }} </form> </div> </div> </div> </div> {% endblock %}
Partager