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

Amigo 2019-09-11 10:13:36 +02:00
parent 3672a1dead
commit e6b40a97f9
1 changed files with 17 additions and 5 deletions

@ -127,17 +127,29 @@ I am going to also pass it the destination of where we have moved the file becau
[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 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. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
`$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
[00:30:16](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h30m16s)
We still need to create remove file, if we have a problem. Why do we need to remove the file? Well all the way up till here(see video) the files is not on the server. It is somewhere in memory. Somewhere in limbo. If something happens [00:30:40](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h30m40s) and we returned false here, we don't need to do anything, it's not really anywhere it's just going to disappear. Same happening here or there or any of these other areas before here. But the moment we do this Move uploaded file $p_file = JFile::up;oad(tmp_src,$tmp_dest, false,true), we are moving the file to the server, if that is true, it is on the server. When we pass the destination and the filename to the 'checkUpload', we already have the file on the server. [00:31:12](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h31m12s) If we then discover that this file is not legit, we don't want it, we must remove it, or any other checks we do, even if it's something more complicated than this, you must remove it, because it's on the server. Our little remove function, I'm trying to save a some unnecessary things. I'm going to sniper it up a bit because already up here [00:31:36](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h31m36s) we have the classes for the file in a folder, so we don't need to load that again. We going to pass it the destination, which is the 'folder', that's going to be fine. All of this(see video) we are able to clear out the name we don't need this(folder) we just need to package name. [00:32:04](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h32m04s) We are going to ask, is it a file? Then delete, then if there's something not right, we are going to do clean path and just check again and delete. This should remove the file from the server. You can test that if something's wrong, just Google it, and tweak it but this should be ok. We are going to pass [00:32:29](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h32m29s) the destination path, which is the one where we moved it, remember up here. That's the same path we are passing all the way down to the 'removeFile' function and then it will clear it out for us. If everything is fine and it doesn't need to, we are going to continue on and then pass it back to the package [00:32:56](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h32m56s) which will be returned to our save method.
We still need to create remove file, if we have a problem. Why do we need to remove the file? Well all the way up till here(see video) the files is not on the server. It is somewhere in memory. [00:30:40](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h30m40s) If something happens and we returned false here, we do not need to do anything, it is not really anywhere it is just going to disappear and the same happens to these other areas (See Video). But the moment this `Move uploaded file $p_file = JFile::up;oad(tmp_src,$tmp_dest, false,true)` is done, we are moving the file to the server, if that is true, it is on the server. When we pass the destination and the filename to the '`checkUpload`', we already have the file on the server. [00:31:12](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h31m12s) If it is then discovered that this file is not legitimate, we must remove it, or any other checks we do, even if it is something more complicated than this, it must be removed, because it is on the server. With this little remove function `(>removeFile`) some unnecessary trouble might be avoided I am going to sniper it up a bit because we have already up here the classes for the file in a folder, so it is not necessary to load that again.[00:31:36](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h31m36s) Then pass it the destination, which is the '`folder`'.
So all of this code that I just wrote, I'll take it and dump it on GitHub as a gist. You could go there and I'll give a link to it, but this is quickly coded, I mean I'm sure that you could improve it. We copy it and paste stuff from other classes [00:33:23](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h33m23s) to get this working, but there's nothing really to extravagant. The only thing that we didn't go into is exactly what does cleaning do, and what does apply do and those kind of things. You can go through the Joomla libraries and check exactly what it does. But for the most part it's just generic PHP functionality. That it would be enough to upload the file. [00:33:57](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h33m57s) So let's give it a spin. Back over here, if we click the X on top of the page, it will take us out of the item. We can just click Looks, then Name and we would have again to select the image. There we have the image. We can click save and see what happens. Oops we have a bunch of errors here. Let's see undefined index. We've got trouble. Let me go see why. I didn't pay close [00:34:36](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h34m36s) attention.
All of this(see video) may be cleared out. We do not need this(`folder`) but only the package name. [00:32:04](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h32m04s) Is it a file? Then delete, then if there is something not right, we are going to do `path::clean` and just check again and delete. This should remove the file from the server. Google it to test if something is wrong, , and tweak it but this should be OK. We are going to pass the destination path, which is the one where we moved it, remember up here. [00:32:29](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h32m29s) That's the same path we are passing all the way down to the 'removeFile' function and then it will clear it for us. If everything is fine and it does not need to, we are going to continue and pass it back to the package which will be returned to our save method.
### Dump Code on GitHub as Gist
[00:33:33](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h33m33s)
So all of this code that I just wrote, gets dumped on GitHub as a gist. This can be quickly coded. Copy it and paste stuff from other classes to get this working, but there is nothing really complicated. [00:33:23](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h33m23s) The only thing that we did not go into is exactly what does `cleaning` do, and what does `apply` do and those kind of things. You can go through the Joomla libraries and check exactly what it does. But for the most part it is just generic PHP functionality. That it would be enough to upload the file.<<<<<<<<<<
[00:33:57](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h33m57s)
So let's give it a spin. Back over here, if we click the X on top of the page, it will take us out of the item. We can just click Looks, then Name and we would have again to select the image. There we have the image. We can click save and see what happens. Oops we have a bunch of errors here. Let's see undefined index. We've got trouble. Let me go see why. I didn't pay close [00:34:36](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h34m36s) attention.
I'm just going to do another var_dump(input); I can get the exact way of most possibly need to do for each. It might want to upload more than one. Or we could force it to just use the first. I think I wrote something about this already somewhere. Let me just check. Think this should do the trick. If it's an array we just want to grab the first value out of the array. If you got multiple fields [00:35:09](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h35m09s) that are uploading files, in the same view you going to have to change this little chunk ($userfile = array_values($userfiles)[0] here. Because then you are going to have to deal with each of those files for each loop inside of this function. Which would mean you could skip all these as they are Global to everyone of them and then from about here(see video), you would add it for each loop. [00:35:39](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h35m39s) Then here you won't echo, well you won't break out, you just grab the error into an array which you would have eventually then pass. Targeting each specific package. Then return package, all the successes as well as the errors and handle it somewhere else. So since we are just uploading one file, I'm not going to [00:36:07](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h36m07s) do all that, just going to grab that first value and we are going to work with that first value. Let's go back and try again. Save. Again we have a problem. Not userfiles but userfile. [00:36:32](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h36m32s) It become so little changes like that can throughout errors. At last we have the file on the server. What I had to do is, I had to take away these points here(see video), I had that one part wrong. It won't need the point, must be without it, and you'll see that now there is our file in our temporal folder. It is on [00:37:03](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h37m03s) the server. We have the path and the name, and all those things. Everything now is good.