Bonjour,
Pour mon site j'utilise le plugin 'Jquery file upload'
Tout fonctionne parfaitement.
Cependant je souhaiterai que l'ajout de mes fichiers se fassent dans des dossiers en fonctions de l'utilisateur.
Car pour le moment tout les fichiers uploadé se font dans le même dossier.
Par exemple si client 1 ajoute des fichiers alors création d'un dossier 'client1' puis ajout des fichiers.
Mais le souci c'est que je n'arrive pas à passer ma variable ($_SESSION['client']->societe()) au plugin.
J'ai trouvé ceci :
Mais je n'arrive pas à récupérer ma variable dans la classe php UploadHandler.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 The formData option (see Options documentation) can be used to set additional form data programmatically, e.g.: $('#fileupload').fileupload({ formData: {example: 'nomdufuturdossier'} });
Si quelqu'un connait ce plugin et peut me filer un coup de main, ça serait sympa =)
ps:https://github.com/blueimp/jQuery-Fi...onal-form-data
Pour vous donner plus d'info, j'arrive à créer un dossier en transmettant manuellement une chaine au constructeur de la classe UploadHandler mais je n'arrive pas à transmettre ma variable PHP (même en utilisant un session start() avant), donc je suppose que je dois la faire passer via le traitement JS mais je ne vois pas vraiment comment
Code JS pour le formulaire ou je souhaiterai transmettre ma variable session
Code contenu dans le fichier index de ChargementClient
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 $(function () { 'use strict'; // Change this to the location of your server-side upload handler: var url = 'ChargementClient/', uploadButton = $('<button/>') .addClass('btn btn-primary') .prop('disabled', true) .text('Chargement...') .on('click', function () { var $this = $(this), data = $this.data(); $this .off('click') .text('Interrompre') .on('click', function () { $this.remove(); data.abort(); }); data.submit().always(function () { $this.remove(); }); }); $('#fileupload').fileupload({ url: url, dataType: 'json', autoUpload: false, acceptFileTypes: /(\.|\/)(gif|jpe?g|png|pdf|mp3|mp4|flv|mpe?g|ai|avi)$/i, maxFileSize: 3000000, // Enable image resizing, except for Android and Opera, // which actually support image resizing, but fail to // send Blob objects via XHR requests: disableImageResize: /Android(?!.*Chrome)|Opera/ .test(window.navigator.userAgent), previewMaxWidth: 100, previewMaxHeight: 100, previewCrop: true }).on('fileuploadadd', function (e, data) { data.context = $('<div/>').appendTo('#files'); $.each(data.files, function (index, file) { var node = $('<p/>') .append($('<span/>').text(file.name)); if (!index) { node .append('<br>') .append(uploadButton.clone(true).data(data)); } node.appendTo(data.context); }); }).on('fileuploadprocessalways', function (e, data) { var index = data.index, file = data.files[index], node = $(data.context.children()[index]); if (file.preview) { node .prepend('<br>') .prepend(file.preview); } if (file.error) { node .append('<br>') .append($('<span class="text-danger"/>').text(file.error)); } if (index + 1 === data.files.length) { data.context.find('button') .text('Téléchargement') .prop('disabled', !!data.files.error); } }).on('fileuploadprogressall', function (e, data) { var progress = parseInt(data.loaded / data.total * 100, 10); $('#progress .progress-bar').css( 'width', progress + '%' ); }).on('fileuploaddone', function (e, data) { $.each(data.result.files, function (index, file) { if (file.error) { var error = $('<span class="text-danger"/>').text(file.error); $(data.context.children()[index]) .append('<br>') .append(error); } }); }).on('fileuploadfail', function (e, data) { $.each(data.files, function (index) { var error = $('<span class="text-danger"/>').text('Erreur : Impossible de télécharger ce fichier'); $(data.context.children()[index]) .append('<br>') .append(error); }); }).prop('disabled', !$.support.fileInput) .parent().addClass($.support.fileInput ? undefined : 'disabled'); }); </script>
Construct de uploadHandler
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 error_reporting(E_ALL | E_STRICT); require('UploadHandler.php'); $upload_handler = new UploadHandler('nomdudossier');
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 function __construct($x,$options = null, $initialize = true, $error_messages = null) { if(!file_exists(dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$x)) mkdir(dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$x); $this->options = array( 'script_url' => $this->get_full_url().'/', 'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/'.$x.'/', 'upload_url' => $this->get_full_url().'/'.$x.'/', 'user_dirs' => false, 'mkdir_mode' => 0755, 'param_name' => 'files', // Set the following option to 'POST', if your server does not support // DELETE requests. This is a parameter sent to the client: 'delete_type' => 'DELETE', 'access_control_allow_origin' => '*', 'access_control_allow_credentials' => false, 'access_control_allow_methods' => array( 'OPTIONS', 'HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' ), 'access_control_allow_headers' => array( 'Content-Type', 'Content-Range', 'Content-Disposition' ), // Enable to provide file downloads via GET requests to the PHP script: // 1. Set to 1 to download files via readfile method through PHP // 2. Set to 2 to send a X-Sendfile header for lighttpd/Apache // 3. Set to 3 to send a X-Accel-Redirect header for nginx // If set to 2 or 3, adjust the upload_url option to the base path of // the redirect parameter, e.g. '/files/'. 'download_via_php' => false, // Read files in chunks to avoid memory limits when download_via_php // is enabled, set to 0 to disable chunked reading of files: 'readfile_chunk_size' => 10 * 1024 * 1024, // 10 MiB // Defines which files can be displayed inline when downloaded: 'inline_file_types' => '/\.(gif|jpe?g|png)$/i', // Defines which files (based on their names) are accepted for upload: 'accept_file_types' => '/.+$/i', // The php.ini settings upload_max_filesize and post_max_size // take precedence over the following max_file_size setting: 'max_file_size' => null, 'min_file_size' => 1, // The maximum number of files for the upload directory: 'max_number_of_files' => null, // Defines which files are handled as image files: 'image_file_types' => '/\.(gif|jpe?g|png)$/i', // Use exif_imagetype on all files to correct file extensions: 'correct_image_extensions' => false, // Image resolution restrictions: 'max_width' => null, 'max_height' => null, 'min_width' => 1, 'min_height' => 1, // Set the following option to false to enable resumable uploads: 'discard_aborted_uploads' => true, // Set to 0 to use the GD library to scale and orient images, // set to 1 to use imagick (if installed, falls back to GD), // set to 2 to use the ImageMagick convert binary directly: 'image_library' => 1, // Uncomment the following to define an array of resource limits // for imagick: /* 'imagick_resource_limits' => array( imagick::RESOURCETYPE_MAP => 32, imagick::RESOURCETYPE_MEMORY => 32 ), */ // Command or path for to the ImageMagick convert binary: 'convert_bin' => 'convert', // Uncomment the following to add parameters in front of each // ImageMagick convert call (the limit constraints seem only // to have an effect if put in front): /* 'convert_params' => '-limit memory 32MiB -limit map 32MiB', */ // Command or path for to the ImageMagick identify binary: 'identify_bin' => 'identify', 'image_versions' => array( // The empty image version key defines options for the original image: '' => array( // Automatically rotate images based on EXIF meta data: 'auto_orient' => true ), // Uncomment the following to create medium sized images: /* 'medium' => array( 'max_width' => 800, 'max_height' => 600 ), */ 'thumbnail' => array( // Uncomment the following to use a defined directory for the thumbnails // instead of a subdirectory based on the version identifier. // Make sure that this directory doesn't allow execution of files if you // don't pose any restrictions on the type of uploaded files, e.g. by // copying the .htaccess file from the files directory for Apache: //'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/thumb/', //'upload_url' => $this->get_full_url().'/thumb/', // Uncomment the following to force the max // dimensions and e.g. create square thumbnails: //'crop' => true, 'max_width' => 80, 'max_height' => 80 ) ) ); if ($options) { $this->options = $options + $this->options; } if ($error_messages) { $this->error_messages = $error_messages + $this->error_messages; } if ($initialize) { $this->initialize(); } }
Partager