Uploading Files to Dynamic Folders

Uploading Files to Dynamic Folders

Learn how to upload files to dynamic folders and store their names into a database

With this movie we will show you how to upload files to dynamic folders and store their names into a database.

Step by Step

We will show you how to upload files to dynamic folders, based on inserted record identity. We created a dynamic table, displaying the records, from a database. Please check the Creating a paged query video, in order to learn how to do this. We also created a simple form, containing 2 text fields and a file field. We'd like to insert a record, create a folder using the inserted record ID, as a folder name and then move the uploaded file into it. This way, each of our products in the database will have its own folder, and all of its files will be inside. First, let's create a server action. We name it insert product (1). then, select globals and import the insert form fields (2). Just as usual - Browse to the page, containing your form and select your form from the dropdown. Then click the Import button (3).

Then, right click steps. And add a database connection (1). Select your database connection, using the folder icon (2).

Then, right click the database connection step. Open file management menu. And add file upload (1). Then, select your upload field (2). Expand Globals, and you will see the upload field, under the post variables. Click the select button (3).

Select the upload path (1). We select a static upload folder, for now. Later, we are going to adjust ,the dynamic folder names. Click the select folder button (2).

Now, right click the upload step. And add a database insert (1). Setup the insert options (2). Select your database table, and add it (3). Then, select the database fields, which you'd like to populate, with data (4). And add them to the columns list. Select the first column. And assign a value to it (5).

We select the first field, of the insert form, under globals, post. And then, click the select button.

We do the same, for the rest of the database columns, selecting the corresponding form fields, from the post variables. When we are done with the rest of the columns, we select the image column. And click the dynamic value button. And then, we select name, from the File Upload step. Click the select button.

And click OK. Now, let's setup the dynamic upload folder names. Right click the database insert step. Open the file management menu. And select - add create folder (1). Click the path button, in order to select, where the folder should be created (2). We select the images folder. Click the select folder button (3).

Now, let's add the dynamic folder name. Click the dynamic data button (1). We'd like to use the identity of the inserted record, as a folder name (2). So we just add it, after the path, that we selected. We enter slash and then we add insert 1, dot, identity wrapped in double curly brackets. Insert 1 is just the name of our insert step so here you should use, whatever you used as a insert record step name. Click the select button (3).

The last thing, that we need to do after the dynamic folder is created is to move the uploaded file, from the static folder into the dynamic folder. Open the file management menu. And select add file move (1). Click the dynamic data button, in order to select the path, where the file should be moved from (2). And under the file upload step, select path. And click the select button (3).

Then, select the folder where the file should be moved to. First we select the static part of the path. Which, in our case is the images folder (1). Then, let's add the dynamic part of the path (2). Here, we enter the same as we entered in the create folder step slash, followed by insert 1, dot, identity wrapped in double curly brackets. Note! insert 1, must be substituted with your insert record step name. Click the select button, when you are done (3). And save your action file (4). Now, let's create a server action executor (5).

Add a name for your executor. then, select to auto run it on form submit. Select your insert server action, from the dropdown bind it to the insert record form and set its action to POST. Click the options button. Open behaviors.

Select control server connect action executor (1). Select it to run on success (2). And add the selected behavior (3).

Here, we select to run the server action executor, which lists the products on our page (1). This way, the data in the table will be refreshed immediately, after insert. Click OK. Then click OK again. And then, click the ok button, in order to create the executor (2).

Let's setup the path, of the dynamic image in the table (1). Select the image (2). And then select the image binding, from the products data source (3). Open the bind to menu. And select image source. And click the bind button.

In the properties inspector, you see that our image source, is bound to the image binding. We need to add the path, to this image. Our path contains two parts - static and dynamic. The static part is the images folder and the dynamic one is the record identity. So we add the path to the static folder, followed by the dynamic folder name, which is equal to the identity of the record. We use the ID binding, from our data source. In our case it is called PID. Let's save the page, and preview the results.

We enter the name and price of our product. Then we select its image and click the add product button. You can see the product appears in the table, as soon as we add it. The product ID is 4. You can see our folder, called 4 inside the images folder, on our server. The image, that we uploaded, is inside the folder. That's how easy it is to upload files, into dynamic folders, on your server.

Extensions Covered

Comments

Be the first to write a comment

You must me logged in to write a comment.