Updated 056 How to use the file field type to upload a file in JCB (markdown)

Amigo 2019-09-11 07:37:58 +02:00
parent df38e46186
commit 98d9dd1059
1 changed files with 5 additions and 9 deletions

@ -120,20 +120,16 @@ We are going to use a little custom script here to validate our format. In my co
[00:26:40](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h26m40s)
I am going to also pass it the destination of where we have moved the file because that is where the file currently is. Here we got the temporal destination(` $tmp_dest` ) which is in the temporal path( `'tmp_path'`), the temporal folder of the Joomla website. It is not where we want to end up. [00:27:02](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h27m02s) It is just where we are going to keep the file until we happy that it is safe. We got it in the temporal destination, we moved it up there, and now we checked if something happened. Do check the upload. Here if we find that this '`archivename`' file [00:27:26](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h27m26s) is problematic, we are going to have to remove it. We are going to have a new little class here. We are going to say `removeFile`. I think that is right. We are going to write this little `removeFile` in a moment. Here is a lot of validation whether the user has permission to upload stuff. It can be removed, because it is going to be a little bit beyond the scope of what is explained. <<<<<<<<<<
I am going to also pass it the destination of where we have moved the file because that is where the file currently is. Here we got the temporal destination(` $tmp_dest` ) which is in the temporal path( `'tmp_path'`), the temporal folder of the Joomla website. It is not where we want to end up. [00:27:02](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h27m02s) It is just where we are going to keep the file until we happy that it is safe. We got it in the temporal destination, we moved it up there, and now we checked if something happened. Do check the upload. Here if we find that this '`archivename`' file [00:27:26](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h27m26s) is problematic, we are going to have to remove it. We are going to have a new little class here. We are going to say `removeFile`. I think that is right. We are going to write this little `removeFile` in a moment. Here is a lot of validation whether the user has permission to upload stuff. It can be removed, because it is going to be a little bit beyond the scope of what is explained.
### Pass A Package Back
[00:28:07](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h28m07s)
Now I want to pass a package back which we are happy with, saying this is where our file is, and what its name is. We are going to do that. That's the directory, and that is the package name(see video). We are going to pass that back to the package. We could since we've got the format and maybe you want to store that. [00:28:41](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h28m41s) We could also add 'format' to the array. Everything looks good. The package name, directory path, as well as the format and we will return that back as the package and we give the package back to the model. In the save function we are going to call this 'myLoadFunction'. [00:29:17](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h29m17s) We said we are going to pass it the 'input' field, the input value, since we're already getting it, why get it again. We are going to pass that over here(see video). [00:29:44](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h29m44s) Now all that needs to be done is over here instead of this function $input->files->get('jform',null,'array') we can now do the following:
$this->myUploadFunction($input). I will end up with that '$userfiles' package [00:30:07](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h30m07s) information right in this variable, and we can then just exit out to see how that will look.
Now I want to pass a package back which we are happy with, saying this is where our file is, and what its name is. We are going to do that. That is the directory, and that is the package name(see video). We are going to pass that back to the package since we have got the format and maybe you want to store that. [00:28:41](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h28m41s) '`format`' could also be added to the array. Everything looks good. The package name, directory path, as well as the format and we will return that back as the package and we give the package back to the model. In the save function we are going to call this '`myLoadFunction`'. [00:29:17](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h29m17s) Pass it the '`input`' field, the input value, since we are already getting it, why get it again. We are going to pass that over here(see video). [00:29:44](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h29m44s) Now all that needs to be done is over here instead of this function `$input->files->get('jform',null,'array')` we can now do the following:
`$this->myUploadFunction($input)`. I will end up with that '`$userfiles`' package information right in this variable, and then just exit out to see how that will look. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
### Create Remove File