Page 1 of 1

Dynamic folder for file upload

Posted: Fri Mar 08, 2024 12:05 pm
by athira

I have to store attachment based on year inside uploads folder. My primary key is not running numbers its is format like EMC-03-24-1. while row_inserting i passed this REQUEST_NO into a session variable. But it is taking in userfn as previously inserted REQUEST_NO. How to fix it? is there any alternative option to upload in a dynamic folder?


Re: file upload

Posted: Fri Mar 08, 2024 2:41 pm
by arbei

What did you mean by "based on year"? If "year" is a field and it always has data, you may try set the field's upload folder as $this->year->DbValue . "/". (You should not use session variable unless you are sure that the session variable will exist when you retrieve the record.)


Re: file upload

Posted: Fri Mar 08, 2024 2:46 pm
by athira

Based on SLNO i want to upload in folder. For example i have a req with REQUEST_NO env-7-24-1 for this particular record have requested date from that date i have to take year and upload based on that year inside uploads folder


Re: file upload

Posted: Fri Mar 08, 2024 2:48 pm
by athira

this is the code inside userfn

function get_emc_date() {
	$page = CurrentPage();
	$emc_date = NULL;
	$emcno = $page->ENV_REQUEST_NO->DbValue;
	if (strlen($emcno) == 0)
		$emc_date = $page->REQUEST_DATE->DbValue;
	else
		$emc_date = ew_ExecuteScalar("SELECT REQUEST_DATE FROM env_test_req_form WHERE ENV_REQUEST_NO = '$emcno'");
	return date('Y', strtotime($emc_date));
}

in attachment filed i added this function value there inside upload folder


Re: Dynamic folder for file upload

Posted: Fri Mar 08, 2024 3:28 pm
by arbei

Then you may put your function in Global Code and use your function as the field's upload folder, e.g. get_emc_date() . "/".


Re: Dynamic folder for file upload

Posted: Mon Mar 11, 2024 2:48 pm
by athira

this is not working properly. i think because of the primary key not Auto Increment. It is like EMC-03-24-1. So I have to store this into session while inserting and session want to use in global code but that also not working properly. now the $emcno = $page->ENV_REQUEST_NO->DbValue; getting null. Is there any idea for that. Thank You


Re: file upload

Posted: Mon Mar 11, 2024 3:37 pm
by arbei

arbei wrote:

You should not use session variable unless you are sure that the session variable will exist when you retrieve the record.

When you insert a new record, you need to make sure the field "ENV_REQUEST_NO" has value. You may use Row_Inserting event to set a value if it is not set yet.


Re: Dynamic folder for file upload

Posted: Mon Mar 11, 2024 4:00 pm
by athira

i set like this $rsnew['ENV_REQUEST_NO'] = 'EMC-'.$month.'-'.$year.'-'.$count; row_inserting it is coming correctly. bt when save into session and call inside userfn then only it is taking previous ENV_REQUEST_NO. not current one.


Re: file upload

Posted: Mon Mar 11, 2024 4:27 pm
by arbei

athira wrote:
i set like this $rsnew['ENV_REQUEST_NO'] = 'EMC-'.$month.'-'.$year.'-'.$count;

For exactly the same reason as:

You should not use session variable unless you are sure that the session variable will exist when you retrieve the record.

You need to make sure the same values of $month, $year, and $count exist when you retrieve the record. (You should not use current month, year and count.)


Re: Dynamic folder for file upload

Posted: Mon Mar 11, 2024 5:05 pm
by athira

then is there any option in row_inserting to save attachment in particular directory


Re: Dynamic folder for file upload

Posted: Tue Mar 12, 2024 10:24 am
by arbei

Each upload field has it upload folder which you can set by $this->MyLoadField->UploadPath. However, similarly, you need to make sure you can get the same upload path when you retrieve the record. (You should not use current month, year and count.)


Re: Dynamic folder for file upload

Posted: Tue Mar 12, 2024 12:16 pm
by athira

$this->ATTACHMENT->'emc_uploads\NEW'; this is showing error


Re: Dynamic folder for file upload

Posted: Tue Mar 12, 2024 12:33 pm
by arbei
  1. You did not use the UploadPath property at all, and you have syntax error in your code,
  2. When you retrieve the record, you need to set the same UploadPath to the field by Row_Rendering server event.

Re: Dynamic folder for file upload

Posted: Tue Mar 12, 2024 12:50 pm
by athira

Sorry actually i did not get how to use this inside row_rendering.