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

Amigo 2019-09-17 08:39:15 +02:00
parent 39d130e8d9
commit 01f872976a
1 changed files with 7 additions and 3 deletions

@ -161,15 +161,19 @@ We can now move the file to its final location. We could deal with moving the fi
[00:38:00](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h38m00s)
We could have more values in here(see video), if we want to, we could do something like this instead of just passing that userfile, pass the whole array, and change that(See video) to archive and add '`name`', which means it is available. So instead of doing check we can then do `$archive ['packagename']`. Then `$archive['dir']`, and [00:38:42](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h38m42s) `$archive['format']`, and return the `$archive` package.<<<<<<<
We could have more values in here(see video), if we want to, instead of just passing that userfile, pass the whole array, and change that(See video) to archive and add '`name`', which means it is available. So instead of doing `check` we can then do `$archive ['packagename']`. Then `$archive['dir']`, and [00:38:42](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h38m42s) `$archive['format']`, and return the `$archive` package.
Now we will have all the information which we were using even up here, and will have all of that information to our disposal. Of coarse also need to change this to that. Well you know what the name is already there [00:39:13](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h39m13s) so instead of package name we can just be satisfied with the name. We could call this set directory $archive['full_path']. That'll be the only information that we are adding which is new with these two values So we can just do that. Now our array looks full. [00:39:44](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h39m44s) It's got the type, the original temporal, obviously that's no longer there. Any errors that might have come up, the size of the file, the full path and the format. We got all those information to use. At the end of the day the only information we are really going to keep is up to us. We could decide to keep the size of the file, and to keep it format. It's really up to us. [00:40:14](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h40m14s) I'm not going to go into how to deal with all that. We just have one name field, column in the database called banner, in which we are to place our final information. That information should be the path in relation to the image folder where we have [00:40:42](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h40m42s) placed this file.
Now we will have all the information at our disposal which has been used up here and of coarse the necessary changes should be done here( Please follow on video). Well, you know what the `name` is already there [00:39:13](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h39m13s) so instead of `package name` we can just be satisfied with the `name`. We could call this `//set directory $archive['full_path']`. That will be the only information that is added which is new with these two values. Now our array looks full. [00:39:44](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h39m44s) It got the 'type', the original temporal, which is obviously no longer there. Any 'errors' that might have come up, the 'size' of the file, the 'full path' and the 'format'. All that information is available. It may be decided which information should be kept as the 'size' of the file or the 'format'. [00:40:14](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h40m14s) We just have one Name field column in the database called 'banner', in which the final information is placed. That information should be the path in relation to the image folder where this file is placed.
### Using Constants
[00:40:48](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h40m48s)
First I'm going to get some constants to use. I like using this JPATH_ROOT as it is always going to be the root path to our website. Then next, add the path to the images. It's where we going to place our image. I'm going to copy JPATH_ROOT, we are going to add the path, the relation to the root folder to the banner in the database. You do realize that this is just for you. It's that you will know where the file is later, you will then on the front end of the site, where you are going to use this value, you will have to [00:41:33](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h41m33s) get the value from the database. You'll still need to create the actual image html tag to load the image or whatever you are going to do with this file. You're still need to code that, nothing of that's going to be done. You are going to have the path to exactly where the file is, because you're storing it that way in the banner column, that's the field. Here we taking the root path 'JPATH_ROOT' [00:42:02](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m02s) and that exact value and we are moving the full path, which we build-up here, which is the current destination. We are going to move it from the full path to the final destination. That is really it. At this point we have got the image on the server and we are moving it to the correct location. We are storing the value [00:42:28](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m28s) in the database, and we've done with saving and uploading the actual value. That would be all that's required. In any of these steps, you might want to do some more things, be more secure, be more validating, because it's not only images you are going to be uploading. If you are uploading PDF, if you are uploading other kind of content, [00:42:56](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m56s) you are going to have to do some googling to anywhere in these steps. Especially in this check upload area, to do the correct validation. At this point the file is on your server, and you need to make sure that it's the right file. Now this isn't airtight. Because this checks that the files type is one of these, the extensions. [00:43:22](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h43m22s) It doesn't validate the file to being an image. There are more secure ways to do that. If you do use them, the server side if your application is going to be running, with shared environments, [00:43:42](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h43m42s) then it will cause problems because some of those functions are not available in all shared environments. But if you want to be very specific, you could help the user along to who uses your application to enable those functions on their server, and by that giving them extra security with uploading of files. This is a very big topic. [00:44:14](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h44m14s) In general this sort of implementation is how most are doing it.
First, get some constants to use. This `JPATH_ROOT` will be used as it is always going to be the root path to our website. Then next, add the path to the images. It is where the image is going to be placed. I am going to copy `JPATH_ROOT`, and add the path, the relation to the root folder to the `banner` in the database. It is only that you will later know where the file is, then on the front end of the site, where you are going to use this value, you will have to get the value from the database. [00:41:33](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h41m33s)<<<<<<<<<<<<<<
You'll still need to create the actual image html tag to load the image or whatever you are going to do with this file. You're still need to code that, nothing of that's going to be done. You are going to have the path to exactly where the file is, because you're storing it that way in the banner column, that's the field. Here we taking the root path 'JPATH_ROOT' [00:42:02](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m02s) and that exact value and we are moving the full path, which we build-up here, which is the current destination. We are going to move it from the full path to the final destination. That is really it. At this point we have got the image on the server and we are moving it to the correct location. We are storing the value [00:42:28](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m28s) in the database, and we've done with saving and uploading the actual value. That would be all that's required. In any of these steps, you might want to do some more things, be more secure, be more validating, because it's not only images you are going to be uploading. If you are uploading PDF, if you are uploading other kind of content, [00:42:56](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h42m56s) you are going to have to do some googling to anywhere in these steps. Especially in this check upload area, to do the correct validation. At this point the file is on your server, and you need to make sure that it's the right file. Now this isn't airtight. Because this checks that the files type is one of these, the extensions. [00:43:22](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h43m22s) It doesn't validate the file to being an image. There are more secure ways to do that. If you do use them, the server side if your application is going to be running, with shared environments, [00:43:42](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h43m42s) then it will cause problems because some of those functions are not available in all shared environments. But if you want to be very specific, you could help the user along to who uses your application to enable those functions on their server, and by that giving them extra security with uploading of files. This is a very big topic. [00:44:14](https://www.youtube.com/watch?v=o482sK4DxkM&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h44m14s) In general this sort of implementation is how most are doing it.
### Successfully Saved - Show No File Detected - Might Show The File Currently In Database