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

Amigo 2019-09-18 09:01:02 +02:00
parent b6c3e30d1d
commit 24503bca50
1 changed files with 4 additions and 3 deletions

@ -177,17 +177,18 @@ First, get some constants to use. This `JPATH_ROOT` will be used as it is always
[00:44:31](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h44m31s)
Everything is moved into place and a trial run can be done. It has been saved successfully but no file selected is shown. We might want to show the file that is currently in the database, instead of this upload or show the upload still; but let's say someone decides to upload another one, that changes it, and remove the old one and adds the new one, and displays the little image as it is in the database. First, let see if it is in the database. [00:45:12](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h45m12s) Our file is in the database, and it is set to be in `/images/autumn_trees_forest_building_grass.jpg.` Let's see if it was moved to the place. In the folder structure, open images, and click on images, then scroll down and see `/images/autumn_trees_forest_building_grass.jpg.` in its place.[00:45:46](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h45m46s) The actual image does now exist in the image folder, it has been uploaded and it is in the database, although the snippet says that it can not see it.
Everything is moved into place and a trial run can be done. It has been saved successfully but no file selected is shown. We might want to show the file that is currently in the database, instead of this upload or show the upload still; but let's say someone decides to upload another one, that changes it, and remove the old one and adds the new one, and displays the little image as it is in the database. First, let see if it is in the database. [00:45:12](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h45m12s) Our file is in the database, and it is set to be in `/images/autumn_trees_forest_building_grass.jpg.` Let's see if it was moved to the place. In the folder structure, open images, and click on images, then scroll down and see `/images/autumn_trees_forest_building_grass.jpg.` in its place.[00:45:46](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h45m46s) The actual image does now exist in the image folder, it has been uploaded and it is in the database, although the snippet says that it can not see it.
### Grab Custom Scripting And Add It To The Component In JCB
[00:46:09](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h46m09s)
Before we continue with that kind of custom scripting, we need to take what we have done so far, and add it to JCB. We have done it in the IDE, and if we compile and install this component now, you will overwrite everything that you have written, and it will be gone. Let's go grab that custom scripting and add it to the component in JCB. (See video) We want to grab everything from my 'uploads function', [00:46:41](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h46m41s) do 'check upload', 'remove file' and say 'cut' and then place that in the clipboard, and go to JCB, in the 'Look Admin Views' which is where all this is happening. If it is opened, there is a place called 'Custom Buttons'. You are going to say 'Yes'. [00:47:07](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m07s)<<<<<<<<<<<
Before we continue with that kind of custom scripting, we need to take what we have done so far and add it to JCB. We have done it in the IDE, and if we compile and install this component now, you will overwrite everything that you have written, and it will be gone. Let's go grab that custom scripting and add it to the component in JCB. (See video) We want to grab everything from my 'uploads function', [00:46:41](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h46m41s) do 'check upload', 'remove file' and say 'cut' and then place that in the clipboard, and go to JCB, in the 'Look Admin Views' which is where all this is happening. If it is opened, there is a place called 'Custom Buttons'. You are going to say 'Yes'. [00:47:07](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m07s)<<<<<<<<<<<
Then there is the PHP(controller method), we are not adding anything there, but there is the PHP(model method), this is the place for us to add some script to the model. We are going to paste what we've cut, right in here, and over here in the PHP List View (controller method) we're not adding anything and also in this PHP List View (model method) [00:47:32](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m32s) we're not adding anything. We are adding that which we have written to the model method, at your PHP here is going to go into the model as methods. This is the methods, that 'checkupload' is a method, that 'myUploadFunction' is a method. We want it to be available to the class from now on. [00:47:59](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m59s) We are not at this point dealing with if you upload a new file that it removed the old one. That should still be further custom scripting. At this point, all add the new file. It won't remove the old one. It could be that it's adding more files and more files and it just gets added to the image folder, but it's not being removed. You have the remove [00:48:26](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h48m26s) file function, so you could fix this quite easily. I'm not going to show you how to do that. I want to encourage you to study up some of your PHP as well.
Then there is the PHP(controller method), nothing is going to be added there, but there is the PHP(model method), this is the place for us to add some script to the model. We are going to paste what we have cut, right in here, and in the PHP List View (controller method) nothing is added and also in this PHP List View (model method) nothing is added. [00:47:32](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m32s) We are adding that which we have written to the model method, '_Add your PHP here!_' is going to go into the model as 'methods'. This is the methods, that '`checkupload`' is a method, that '`myUploadFunction`' is a method. We want it to be available to the class from now on. [00:47:59](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h47m59s) We are not at this point dealing with if you upload<<<<<
a new file that it removed the old one. That should still be further custom scripting. At this point, all add the new file. It won't remove the old one. It could be that it's adding more files and more files and it just gets added to the image folder, but it's not being removed. You have the remove [00:48:26](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h48m26s) file function, so you could fix this quite easily. I'm not going to show you how to do that. I want to encourage you to study up some of your PHP as well.
### Adding the Script To The Save Method